Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/redis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Google maps api 3 使用Google Places API获得20多个结果_Google Maps Api 3_Google Places Api - Fatal编程技术网

Google maps api 3 使用Google Places API获得20多个结果

Google maps api 3 使用Google Places API获得20多个结果,google-maps-api-3,google-places-api,Google Maps Api 3,Google Places Api,我想开发一个地图应用程序,它将显示指定地点附近的银行 我使用Places库进行搜索,每次只返回20个结果。如果我想要更多的结果,我该怎么办?不确定你能得到更多 Places API最多返回20个建立结果 更新:由于我最初编写了此答案,API得到了增强,使此答案过时(或至少不完整)。有关更多信息,请参阅 原始答案: 报告说,Places API最多返回20个结果。这并不表明有任何方法可以改变这一限制。所以,简单的答案似乎是:你不能 当然,您可以通过查询多个位置,然后合并/消除重复结果来对其进行排序

我想开发一个地图应用程序,它将显示指定地点附近的银行


我使用Places库进行搜索,每次只返回20个结果。如果我想要更多的结果,我该怎么办?

不确定你能得到更多

Places API最多返回20个建立结果


更新:由于我最初编写了此答案,API得到了增强,使此答案过时(或至少不完整)。有关更多信息,请参阅

原始答案

报告说,Places API最多返回20个结果。这并不表明有任何方法可以改变这一限制。所以,简单的答案似乎是:你不能


当然,您可以通过查询多个位置,然后合并/消除重复结果来对其进行排序。不过,这是一种廉价的黑客,可能不会很好地工作。我会首先检查以确保它没有违反服务条款。

现在有可能有20个以上的结果(但最多60个),API中添加了一个参数

返回以前运行的搜索的下20个结果。设置页面标记参数将使用以前使用的相同参数执行搜索-将忽略页面标记以外的所有参数


您也可以参考本节来查看如何进行分页的示例。

为了回应Eduardo,Google现在添加了这一点,但Places文档也说明:

可以返回的最大结果数为60

所以它还有一顶帽子。另外,供参考,“下一页令牌”将延迟生效,如谷歌所述:

在下一个_页面_令牌发出和生效之间有一个短暂的延迟

以下是API文档的官方位置:


如果问题是并非搜索范围内存在的所有银行都可能无法返回,我建议限制搜索范围,而不是发出多个自动查询

将半径设置为不可能获得超过20(60)个气缸组的某个值。然后让用户可以轻松地(在GUI方面)手动完成更多的查询,就像绘制查询一样


在一个更大的区域内返回数千家银行可能需要您依靠自己的银行数据库—如果您系统地进行工作,这是可以实现的。

下面的代码示例将查找其他结果

using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Web.Script.Serialization;


namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            var url = $"https://maps.googleapis.com/maps/api/place/nearbysearch/json?location={args[0]}&radius={args[1]}&type=restaurant&keyword={args[2]}&key={args[3]}";

            dynamic res = null;
            var places = new List<PlacesAPIRestaurants>();
            using (var client = new HttpClient())
            {
                while (res == null || HasProperty(res, "next_page_token"))
                {
                    if (res != null && HasProperty(res, "next_page_token"))
                    {
                        if (url.Contains("pagetoken"))
                            url = url.Split(new string[] { "&pagetoken=" }, StringSplitOptions.None)[0];
                        url += "&pagetoken=" + res["next_page_token"];

                    }
                    var response = client.GetStringAsync(url).Result;
                    JavaScriptSerializer json = new JavaScriptSerializer();
                    res = json.Deserialize<dynamic>(response);
                    if (res["status"] == "OK")
                    {
                        foreach (var place in res["results"])
                        {
                            var name = place["name"];
                            var rating = HasProperty(place,"rating") ? place["rating"] : null;
                            var address = place["vicinity"];
                            places.Add(new PlacesAPIRestaurants
                            {
                                Address = address,
                                Name = name,
                                Rating = rating
                            });
                        }
                    }
                    else if (res["status"] == "OVER_QUERY_LIMIT")
                    {
                        return;
                    }
                }
            }
        }

        public static bool HasProperty(dynamic obj, string name)
        {
            try
            {
                var value = obj[name];
                return true;
            }
            catch (KeyNotFoundException)
            {
                return false;
            }
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Net.Http;
使用System.Web.Script.Serialization;
命名空间控制台应用程序1
{
班级计划
{
静态void Main(字符串[]参数)
{
var url=$”https://maps.googleapis.com/maps/api/place/nearbysearch/json?location={args[0]}&radius={args[1]}&type=restaurant&keyword={args[2]}&key={args[3]}”;
动态res=null;
var places=新列表();
使用(var client=new HttpClient())
{
while(res==null | | HasProperty(res,“下一页标记”))
{
if(res!=null&&HasProperty(res,“下一页标记”))
{
if(url.Contains(“pagetoken”))
url=url.Split(新字符串[]{“&pagetoken=“},StringSplitOptions.None)[0];
url+=“&pagetoken=“+res[“下一页”标记“];
}
var response=client.GetStringAsync(url).Result;
JavaScriptSerializer json=新的JavaScriptSerializer();
res=json.反序列化(响应);
如果(res[“状态”]=“正常”)
{
foreach(在res[“结果”]中的var位置)
{
变量名称=地点[“名称”];
var评级=HasProperty(地点,“评级”)?地点[“评级”]:空;
var地址=地点[“附近”];
places.Add(新的PlacesAPIRestaurants)
{
地址,
Name=Name,
评级=评级
});
}
}
否则如果(res[“status”]=“OVER\u QUERY\u LIMIT”)
{
返回;
}
}
}
}
公共静态布尔属性(动态对象,字符串名称)
{
尝试
{
var值=对象[名称];
返回true;
}
catch(KeyNotFoundException)
{
返回false;
}
}
}
}

希望这能为您节省一些时间。

您可以刮取Google Places结果,然后按照分页方式为特定位置获取200-300个位置(10到15页搜索结果)

或者,您可以使用访问从Google Places提取的数据。它是免费试用的

#包:https://pypi.org/project/google-search-results
从serpapi导入谷歌搜索
导入操作系统
参数={
“api_键”:os.getenv(“api_键”),
“引擎”:“谷歌”,
“q”:“餐厅”,
“地点”:“美国”,
“tbm”:“lcl”,
“开始”:0
}
搜索=谷歌搜索(参数)
data=search.get_dict()
对于数据中的本地结果[“本地结果”]:
印刷品(
f“位置:{local_result['Position']}\n标题:{local_result['title']}\n”
)
while('next'在数据中['serpapi_pagination']):
search.params_dict[“start”]+=len(数据['local_results'])
data=search.get_dict()
打印(f“当前页面:{data['serpapi_pagination']['Current']}\n”)
对于数据中的本地结果[“本地结果”]:
印刷品(
f“位置:{local_result['positi]
Current page: 11

Position: 1
Title: Carbone

Position: 2
Title: Elmer's Restaurant (Palm Springs, CA)

Position: 3
Title: The Table Vegetarian Restaurant

...