Google Places API - android

后端 未结 2 1996
情话喂你
情话喂你 2020-12-12 07:27

I am making an android application that needs to search in my local area within 10km and display the results onto a map using pins, For example: \"Starbucks\", \"Wallmart\",

2条回答
  •  时光取名叫无心
    2020-12-12 07:40

    You can do this by using Google API JAVA Client - Here is an example using the java client for getting all the 60 results.

    public PlacesList search(double latitude, double longitude, double radius, String types)
                throws Exception {
    
            try {
    
                HttpRequestFactory httpRequestFactory = createRequestFactory(HTTP_TRANSPORT);
                HttpRequest request = httpRequestFactory
                        .buildGetRequest(new GenericUrl("https://maps.googleapis.com/maps/api/place/search/json?"));
                request.getUrl().put("key", YOUR_API_KEY);
                request.getUrl().put("location", latitude + "," + longitude);
                request.getUrl().put("radius", radius); 
                request.getUrl().put("sensor", "false");
                request.getUrl().put("types", types);
    
                PlacesList list = request.execute().parseAs(PlacesList.class);
    
                if(list.next_page_token!=null || list.next_page_token!=""){
                             Thread.sleep(4000);
                             /*Since the token can be used after a short time it has been  generated*/
                    request.getUrl().put("pagetoken",list.next_page_token);
                    PlacesList temp = request.execute().parseAs(PlacesList.class);
                    list.results.addAll(temp.results);
    
                    if(temp.next_page_token!=null||temp.next_page_token!=""){
                        Thread.sleep(4000);
                        request.getUrl().put("pagetoken",temp.next_page_token);
                        PlacesList tempList =  request.execute().parseAs(PlacesList.class);
                        list.results.addAll(tempList.results);
                  }
    
                }
                return list;
    
            } catch (HttpResponseException e) {
                return null;
            }
    
        }
    

提交回复
热议问题