使用google places api,每次请求最多可获得20个结果。为什么会这样?
我可以通过任何其他方法增加结果数量吗?
答案 0 :(得分:2)
20是最大值。你可以做的是做几个半径不同的请求,并将结果合并/过滤。
答案 1 :(得分:2)
您可以编写一种方法,一次获得60个结果(每页20个),但是获得所有结果会有延迟 -
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;
}
}