如何防止 v3 中的 Google Maps API OVER_QUERY_LIMIT 错误?
Nov 02, 2024 pm 10:50 PM如何处理 Google Maps API v3 中的 OVER_QUERY_LIMIT 错误
简介:
在 Google Maps API v3 中,您可以短时间内发出过多地理编码请求时遇到 OVER_QUERY_LIMIT 错误。为了避免此错误,需要在请求之间实现延迟机制。
JavaScript 实现:
要在地理编码调用之间引入暂停,您可以使用以下 JavaScript代码:
<code class="javascript">function codeAddress(vPostCode) { if (geocoder) { // If there's an existing delay, wait for it to finish while (wait) { /* Just 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); // Set a delay of 2000 milliseconds } else { alert("Geocode was not successful for the following reason: " + status); } }); } }</code>
登录后复制
此代码检查现有延迟,如果发生 OVER_QUERY_LIMIT 错误,则设置新的延迟,并等待延迟完成后再恢复地理编码。
示例:
在您提供的代码中,您可以将现有的 codeAddress 函数替换为更新版本:
<code class="javascript">function codeAddress(vPostCode) { if (geocoder) { while (wait) { /* Just 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中文网其他相关文章!
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热门文章
击败分裂小说需要多长时间?
3 周前
By DDD
仓库:如何复兴队友
3 周前
By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒险:如何获得巨型种子
3 周前
By 尊渡假赌尊渡假赌尊渡假赌
公众号网页更新缓存难题:如何避免版本更新后旧缓存影响用户体验?
3 周前
By 王林
R.E.P.O.能量晶体解释及其做什么(黄色晶体)
1 周前
By 尊渡假赌尊渡假赌尊渡假赌

热门文章
击败分裂小说需要多长时间?
3 周前
By DDD
仓库:如何复兴队友
3 周前
By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒险:如何获得巨型种子
3 周前
By 尊渡假赌尊渡假赌尊渡假赌
公众号网页更新缓存难题:如何避免版本更新后旧缓存影响用户体验?
3 周前
By 王林
R.E.P.O.能量晶体解释及其做什么(黄色晶体)
1 周前
By 尊渡假赌尊渡假赌尊渡假赌

热门文章标签

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)