【问题标题】:Places API Call via Google Scripts returns ZERO_RESULTS通过 Google 脚本调用 Places API 返回 ZERO_RESULTS
【发布时间】:2019-12-13 00:05:38
【问题描述】:

我正在尝试使用 Google 脚本和 URLFetchApp 解析来自 Google Place API 调用的 JSON 结果。

我已启用 Places API 并创建了一个 API 密钥,如果我将以下内容粘贴到 Chrome 浏览器中...

https://maps.googleapis.com/maps/api/place/findplacefromtext/json?input=telstra%20headoffice&inputtype=textquery&fields=formatted_address&key=VALID_KEY

我收到:

{
   "candidates" : [
      {
         "formatted_address" : "240 Exhibition St, Melbourne VIC 3000, Australia"
      }
   ],
   "status" : "OK"
}

但是,当我尝试使用 Google 脚本检索它时...

function mapAddress() {
 var url = "https://maps.googleapis.com/maps/api/place/findplacefromtext/json?input=telstra%20headoffice&inputtype=textquery&fields=formatted_address&key=VALID_KEY";
 var response = UrlFetchApp.fetch(url,{muteHttpExceptions:true});
 var json = response.getContentText();
 var data = JSON.parse(json);
 Logger.log(data.status);
 Logger.log(response);

当我查看执行记录时,这是报告的内容...

[19-12-13 10:43:50:300 AEDT] Starting execution
[19-12-13 10:43:50:666 AEDT] UrlFetchApp.fetch([https://maps.googleapis.com/maps/api/place/findplacefromtext/json?input=telstra%20headoffice&inputtype=textquery&fields=formatted_address&key=VALID_KEY, {muteHttpExcepti...) [0.354 seconds]
[19-12-13 10:43:50:666 AEDT] UrlFetchApp.HTTPResponse.getContentText() [0 seconds]
[19-12-13 10:43:50:668 AEDT] Logger.log([ZERO_RESULTS, []]) [0 seconds]
[19-12-13 10:43:50:669 AEDT] Logger.log([{
   "candidates" : [],
   "status" : "ZERO_RESULTS"
}
, []]) [0 seconds]
[19-12-13 10:43:50:670 AEDT] Execution succeeded [0.36 seconds total runtime]

我做错了什么?

【问题讨论】:

  • 我尝试使用您在 Chrome 浏览器中提供的 URL,但得到相同的结果 ZERO_RESULTS,您可能应该再次检查在 Apps 脚本中使用之前的 URL。
  • 它必须与搜索的区域或位置有关。我在澳大利亚。 Google 脚本将从 GCP 位置执行。
  • 我重新访问了 Place Search 的文档,并且有一个 locationbias 字段。它还指出,默认值是基于原始查询的 IP 地址!包括围绕澳大利亚中心的位置偏差解决了这个问题...maps.googleapis.com/maps/api/place/findplacefromtext/…

标签: google-apps-script google-places-api


【解决方案1】:

Places API 文档包含有关位置偏差的信息。默认情况下,它使用原始请求的 IP 地址。

https://developers.google.com/places/web-service/search#PlaceSearchRequests

我使用了基于澳大利亚纬度/对数的位置偏差,现在它可以工作了。

https://maps.googleapis.com/maps/api/place/findplacefromtext/json?input=telstra%20headoffice&inputtype=textquery&fields=place_id,formatted_address&region=au&locationbias=circle:50000@-25.966828,134.537373&key=VALID_KEY

【讨论】:

  • 很高兴您能找到答案,感谢您分享并解释您是如何找到解决方案的
猜你喜欢
  • 1970-01-01
  • 2018-05-26
  • 1970-01-01
  • 1970-01-01
  • 2019-10-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多