簡介:
簡介:在Google Maps可以短時間內發出過多地理編碼請求時遇到OVER_QUERY_LIMIT 錯誤。為了避免此錯誤,需要在請求之間實現延遲機制。
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>要在地理編碼呼叫之間引入暫停,您可以使用以下JavaScript程式碼:
此程式碼檢查現有延遲,如果發生OVER_QUERY_LIMIT 錯誤,則設定新的延遲,並等待延遲完成後再恢復地理編碼。
範例:<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>在您提供的程式碼中,您可以將現有的codeAddress 函數替換為更新版本:此修改將引入必要的延遲以防止OVER_QUERY_LIMIT 錯誤。
以上是如何防止 v3 中的 Google Maps API OVER_QUERY_LIMIT 錯誤?的詳細內容。更多資訊請關注PHP中文網其他相關文章!