
如何处理 Google Maps API v3 中的 OVER_QUERY_LIMIT 错误
简介:
在 Google Maps API v3 中,您可以短时间内发出过多地理编码请求时遇到 OVER_QUERY_LIMIT 错误。为了避免此错误,需要在请求之间实现延迟机制。
JavaScript 实现:
要在地理编码调用之间引入暂停,您可以使用以下 JavaScript代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | <code class = "javascript" > function codeAddress(vPostCode) {
if (geocoder) {
while (wait) {
}
geocoder.geocode({ 'address' : "'" + vPostCode + "'" }, function (results, status) {
if (status == google.maps.GeocoderStatus.OK) {
map.setCenter(results[0].geometry.location);
var marker = new google.maps.Marker({
map: map,
position: results[0].geometry.location
});
} else if (status == google.maps.GeocoderStatus.OVER_QUERY_LIMIT) {
wait = true;
setTimeout( function () {
wait = false;
}, 2000);
} else {
alert( "Geocode was not successful for the following reason: " + status);
}
});
}
}</code>
|
登录后复制
此代码检查现有延迟,如果发生 OVER_QUERY_LIMIT 错误,则设置新的延迟,并等待延迟完成后再恢复地理编码。
示例:
在您提供的代码中,您可以将现有的 codeAddress 函数替换为更新版本:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | <code class = "javascript" > function codeAddress(vPostCode) {
if (geocoder) {
while (wait) { };
geocoder.geocode( { 'address' : "'" + vPostCode + "'" }, function (results, status) {
if (status == google.maps.GeocoderStatus.OK) {
map.setCenter(results[0].geometry.location);
var marker = new google.maps.Marker({
map: map,
position: results[0].geometry.location
});
} else if (status == google.maps.GeocoderStatus.OVER_QUERY_LIMIT) {
wait = true;
setTimeout( function () {
wait = false;
}, 2000);
} else {
alert( "Geocode was not successful for the following reason: " + status);
}
});
}
}</code>
|
登录后复制
此修改将引入必要的延迟以防止 OVER_QUERY_LIMIT 错误。
以上是如何防止 v3 中的 Google Maps API OVER_QUERY_LIMIT 错误?的详细内容。更多信息请关注PHP中文网其他相关文章!