我在使用 google 地图地理编码功能时收到此错误消息,据我所知,当我超过一天 2500 个请求的免费限制时,就会发生这种情况。不过,我已经设置了一个计费选项来为额外的请求支付额外费用,但我仍然收到此错误。当我设置账单时,它要求我创建一个与我的账单信息相关联的项目,我这样做了,然后我得到了一个可以使用的 API 密钥。
我不知道是否/将这个 API 密钥放在我的代码中。这是我的代码片段
var addressColumn = 1;
var addressRow;
var latColumn = addressColumn + 1;
var lngColumn = addressColumn + 2;
var geocoder = Maps.newGeocoder().setRegion(getGeocodingRegion());
var location;
for (addressRow = 1; addressRow <= cells.getNumRows(); ++addressRow) {
var address = cells.getCell(addressRow, addressColumn).getValue();
// Geocode the address and plug the lat, lng pair into the
// 2nd and 3rd elements of the current range row.
location = geocoder.geocode(address);
// Only change cells if geocoder seems to have gotten a
// valid response.
if (location.status == 'OK') {
lat = location["results"][0]["geometry"]["location"]["lat"];
lng = location["results"][0]["geometry"]["location"]["lng"];
cells.getCell(addressRow, latColumn).setValue(lat);
cells.getCell(addressRow, lngColumn).setValue(lng);
}
}
};
有什么想法可以将我的付费计费项目链接到此代码,以便我可以增加 API 的使用吗?
您必须在工作表更改时触发此功能,否则此功能会触发太多时间。此外,我还添加了一个计时器来减轻对第二速率限制的请求。
function getLatLng(address) {
if(address=="")return("");
var API_KEY = "YOUR API KEY";
var options = {
muteHttpExceptions: true,
contentType: "application/json",
};
var serviceUrl = "https://maps.googleapis.com/maps/api/geocode/json?address=" + address + "&key=" + API_KEY;
try{
var response = UrlFetchApp.fetch(serviceUrl, options);
//sleep random between 2 and 1 second for request
var milliseconds = Math.floor(((Math.random() * 2) + 1) * 1000);
Utilities.sleep(milliseconds);
if (response.getResponseCode() == 200) {
var location = JSON.parse(response.getContentText());
if (location["status"] == "OK") {
var lat = location["results"][0]["geometry"]["location"]["lat"];
var lng = location["results"][0]["geometry"]["location"]["lng"];
return([[lat,lng]]);
}
else{
return([["error",location["status"]]]);
}
}
}
catch(err){
return([["error",err.message]]);
}
};
您可以获得 api 密钥here https://developers.google.com/maps/documentation/javascript/get-api-key。
您可以直接在工作表单元格中使用此函数,例如:getLatLng(D2)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)