如何从Google Places API获得20+结果?

Sco*_*ion 17 android google-places-api google-places

我正在开发一个应用程序,我在其中获取用户附近的ATM列表.为此,我使用的是Google Places API,但每次只返回20个结果.我希望得到更多结果.在API文档中提到它将返回20个结果,但我想知道有什么方法可以获得更多数据,或者我必须为此创建自己的数据库?我在API中发现的是它没有印度的所有ATM.某些位置的信息也不准确.那么请告诉我一些想法,我应该使用自己的数据库?我知道我可以在Google地方信息中注册地点,但问题是每次只返回20个结果; 如果无论如何这个问题都解决了,那么使用Places API会很棒.

请帮助我.....

Chr*_*een 17

根据文档:http: //code.google.com/intl/en/apis/maps/documentation/places/#PlaceSearchResponses

编辑: Place API现在支持最多60个结果的分页.请参阅下文了解详情.

Places API每个查询最多可返回20个机构; 但是,每个搜索可以返回多达60个结果,分为三个页面.如果您的搜索返回超过20,则搜索响应将包含一个附加参数 - next_page_token.将next_page_token的值传递给新地点搜索的page_token参数,以查看下一组20个结果.

还有严格的服务条款,指定您无法预取,缓存或存储内容,除非它是您允许存储的内容标识符或密钥,即来自地点搜索的参考令牌:http: //code.google.com /intl/en/apis/maps/terms.html#section_10_1_3

关于无法在印度找到ATM的问题.所有的地方都下型分类establishment直到谷歌拥有约一个地方足够的元数据来对其进行分类下更具体的地方类型,如ATM,cafe,bar等一个解决可能会找到更多的结果是使用keyword的东西,如"ATM"参数在您的要求,'银行'或'财务'作为价值.根据文档:http://code.google.com/apis/maps/documentation/places/keyword参数与所有可用字段匹配,包括但不限于名称,类型和地址,以及客户评论和其他第三方内容.

  • 用于访问下一个20结果的参数应该是"pagetoken",其值为next_page_token. (2认同)

Rus*_*abh 17

Google API会在一个页面中提取20个结果,假设您要使用下一页20结果,那么我们将使用google首页xml中的next_page_token作为结果.

1) https://maps.googleapis.com/maps/api/place/search/xml?location=Enter latitude,Enter Longitude&radius=10000&types=store&hasNextPage=true&nextPage()=true&sensor=false&key=Enter Google_Map_key

在第二步中,您使用第一页的next_page_token数据

2)https://maps.googleapis.com/maps/api/place/search/xml?location=Enter Latitude,Enter Longitude&radius=10000&types=store&hasNextPage=true&nextPage()=true&sensor=false&key=enter google_map_key &pagetoken="Enter the first page token Value"


小智 6

您可以使用Google API JAVA客户端执行此操作 - 以下是使用Java客户端获取所有60个结​​果的示例.

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

    }
Run Code Online (Sandbox Code Playgroud)

  • 谢谢 - 你解决了我的问题。下一页无法立即访问!我看到了你的“Thread.sleep(4000);” 并意识到为什么 pagetoken 有时只在我的代码中起作用 - 通常代码在请求下一页时速度太快。当然,当我通过逐步调试它时,它总是有效。 (2认同)