Javascript 由于 over_query_limit 导致 Google 地理编码器失败
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10574824/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Google geocoder failing due to over_query_limit
提问by M4bhat
I'm filling four form fields using reverse geocoding. The form fills correctly but then I receive an error stating OVER_QUERY_LIMIT. On checking the JS console of chrome I see that five identical calls have been made before I received the error message. I'm unsure why my code is making several requests. Could someone please point out what I should change in my loops in the codeLatLng function. That's most likely where the problem is.
我正在使用反向地理编码填充四个表单字段。表单填写正确,但随后我收到一条错误消息,指出 OVER_QUERY_LIMIT。在检查 chrome 的 JS 控制台时,我看到在收到错误消息之前已经进行了五个相同的调用。我不确定为什么我的代码会发出几个请求。有人可以指出我应该在 codeLatLng 函数的循环中更改什么。这很可能是问题所在。
var geocoder;
function getCity() {
geocoder = new google.maps.Geocoder();
}
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(successFunction, errorFunction);
}
//Get the latitude and the longitude;
function successFunction(position) {
var lat = position.coords.latitude;
var lng = position.coords.longitude;
//alert("Success funciton" + lat + " " + lng);
codeLatLng(lat, lng)
}
function errorFunction(){
AppMobi.notification.alert("For the best user experience, please ensure your device GPS and geolocation settings are enabled.","Geolocation disabled","OK");
}
function codeLatLng(lat, lng) {
//alert("codeLatLng fn started");
var latlng = new google.maps.LatLng(lat, lng);
geocoder.geocode({'latLng': latlng}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
console.log(results)
if (results[1]) {
//formatted address
//alert(results[0].formatted_address) //Complete address
$("#address1").val(results[0].formatted_address);
} else {
alert("No results found. Please enter the data manually.");
}
} else {
alert("Geocoder failed due to: " + status +". Please enter the data manually." );
}
});
Many Thanks in advance.
提前谢谢了。
回答by TlmaK0
Use of the Google Maps API Web Services is subject to specific limits on the amount of requests per day allowed.
Google Maps API Web Services 的使用受每天允许的请求量的特定限制。
OVER_QUERY_LIMIT is caused by this restriction.
OVER_QUERY_LIMIT 是由这个限制引起的。
Here you can find more information:
您可以在这里找到更多信息:
https://developers.google.com/maps/documentation/business/articles/usage_limits
https://developers.google.com/maps/documentation/business/articles/usage_limits
You can try to wait over 2 seconds when google responds with this error, for example, with a setTimeout.
您可以尝试在 google 响应此错误时等待 2 秒以上,例如使用 setTimeout。
It could be that results[0].address_components[i] will be a reference to a function call.
可能结果 [0].address_components[i] 将是对函数调用的引用。