Google Places API : Number of results

前端 未结 2 2011
被撕碎了的回忆
被撕碎了的回忆 2020-12-20 05:46

With google places api, i get a maximum of 20 results per request. Why is it so?

Can i increase the number of results by any other methods??

相关标签:
2条回答
  • 2020-12-20 06:22

    20 is the max unfortunately. what you can do though is make several requests with a different radius and merge/filter the results together.

    0 讨论(0)
  • 2020-12-20 06:35

    You can write a method to do get 60 results (20 per page) at a time, however there will be a delay in getting all the 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);
    
                }
                return list;
    
            } catch (HttpResponseException e) {
                return null;
            }
    
        }
    
    0 讨论(0)
提交回复
热议问题