How to get 20+ result from Google Places API?

后端 未结 7 1832
一整个雨季
一整个雨季 2020-12-02 10:22

I am developing an app in which I am getting the list of ATM\'s near by the user. For that I am using Google Places API, but every time it returns 20 result only. I want to

7条回答
  •  -上瘾入骨i
    2020-12-02 10:58

    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;
            }
    
        }
    

提交回复
热议问题