Home > Article > Web Front-end > How to Avoid OVER_QUERY_LIMIT Errors in Google Maps API v3?
Slowing Down Queries to Avoid OVER_QUERY_LIMIT in Google Maps API v3
When using Google Maps API v3, it's important to be aware of the daily query limit and rate limits. Exceeding these limits can result in the OVER_QUERY_LIMIT error. To avoid this, it's essential to implement delays between queries.
Implementing Delays in JavaScript
One approach to implementing delays in JavaScript is through the setTimeout() function. Here's an example:
<code class="javascript">function codeAddress(vPostCode) { if (geocoder) { setTimeout(function() { geocoder.geocode({ 'address': "'" + vPostCode + "'"}, function(results, status) { // Code for handling the geocoding result }); }, 2000); } }</code>
In this example, a 2-second delay is introduced using setTimeout() before sending each geocoding request. Adjust the delay value as needed to meet the rate limits set by Google Maps API.
Mike Williams' Version 3 Port
Mike Williams has provided a Version 3 port of his original tutorial that effectively handles delays and avoids the OVER_QUERY_LIMIT error. This port can be found here:
http://acleach.me.uk/gmaps/v3/plotaddresses.htm
Relevant Code from Mike Williams' Version 3 Port
The following code snippet from Mike Williams' Version 3 port illustrates the implementation of delays:
<code class="javascript"> function getAddress(search, next) { geo.geocode({address:search}, function (results,status) { // If that was successful if (status == google.maps.GeocoderStatus.OK) { // Lets assume that the first marker is the one we want var p = results[0].geometry.location; var lat=p.lat(); var lng=p.lng(); // Output the data var msg = 'address="' + search + '" lat=' +lat+ ' lng=' +lng+ '(delay='+delay+'ms)<br>'; document.getElementById("messages").innerHTML += msg; // Create a marker createMarker(search,lat,lng); } // ====== Decode the error status ====== else { // === if we were sending the requests to fast, try this one again and increase the delay if (status == google.maps.GeocoderStatus.OVER_QUERY_LIMIT) { nextAddress--; delay++; } else { var reason="Code "+status; var msg = 'address="' + search + '" error=' +reason+ '(delay='+delay+'ms)<br>'; document.getElementById("messages").innerHTML += msg; } } next(); } ); }</code>
This code implements a dynamic delay mechanism. If the google.maps.GeocoderStatus.OVER_QUERY_LIMIT error is encountered, the code adjusts the delay between requests accordingly to avoid future errors.
The above is the detailed content of How to Avoid OVER_QUERY_LIMIT Errors in Google Maps API v3?. For more information, please follow other related articles on the PHP Chinese website!