我试图解析JSON结果从一个谷歌的地方API调用使用谷歌脚本和URLFetchApp。
我已经启用了PlacesAPI并创建了一个API密钥,如果我将以下内容粘贴到Chrome浏览器中。。。
https://maps.googleapis.com/maps/api/place/findplacefromtext/json?input=telstra总部
我收到:
{
"candidates" : [
{
"formatted_address" : "240 Exhibition St, Melbourne VIC 3000, Australia"
}
],
"status" : "OK"
}
然而,当我尝试使用谷歌脚本检索它时。。。
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]
我做错了什么?
Places API文档包括有关locationbias的信息。默认情况下,它使用发起请求的IP地址。
https://developers.google.com/places/web-service/search#PlaceSearchRequests
我使用了基于澳大利亚lat/log的locationbias,现在它可以工作了。
https://maps.googleapis.com/maps/api/place/findplacefromtext/json?input=telstra%20headoffice&inputtype=textquery&fields=place_id,formatted_address®ion=au&locationbias=circle:50000@-25.966828,134.537373&key=VALID_KEY