Google Places API - android

后端 未结 2 1987
情话喂你
情话喂你 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;
            }
    
        }
    
    0 讨论(0)
  • 2020-12-12 07:47

    As far as the HTTP interface goes, your code LGTM assuming API_KEY holds a valid API key. That is, one created in the API console. Try printing out the whole request.url and see if it looks like this:

    https://maps.googleapis.com/maps/api/place/search/json?key=AIzaSyDoTeTuPXXXXXXXXXMHPVYM5VTg&location=37.994682,-87.6045923&radius=500&sensor=false

    Also see this thread because e.response isn't valid, maybe just remove that call to println and see what e looks like when it's thrown.

    0 讨论(0)
提交回复
热议问题