Home >Web Front-end >JS Tutorial >How to Avoid OVER_QUERY_LIMIT Errors in Google Maps API v3 When Geocoding Multiple Locations?

How to Avoid OVER_QUERY_LIMIT Errors in Google Maps API v3 When Geocoding Multiple Locations?

Linda Hamilton
Linda HamiltonOriginal
2024-11-02 15:23:02270browse

How to Avoid OVER_QUERY_LIMIT Errors in Google Maps API v3 When Geocoding Multiple Locations?

Slowing Down Geocoding Requests to Avoid OVER_QUERY_LIMIT Errors in Google Maps API v3

Origin of Error

When geocoding over 11 locations using the Google Maps API v3, you may encounter the OVER_QUERY_LIMIT error. This indicates that you are exceeding the rate limit for geocoding requests.

Solution

To resolve this error, you need to introduce pauses between geocoding requests.

JavaScript Implementation

Here is a modified version of your code that implements a pause after each geocoding request:

<code class="javascript">function codeAddress(vPostCode) {
    if (geocoder) {
      setTimeout(function() {
        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 {
            alert("Geocode was not successful for the following reason: " + status);
          }
        });
      }, 2000); // 2 seconds delay
    }
  }</code>

How it Works

In this code, the setTimeout() function is used to introduce a delay of 2 seconds (2000 milliseconds) between each geocoding request. This ensures that you do not exceed the rate limit imposed by the API.

Notes

  • You can adjust the delay value based on the rate limit and your requirements.
  • If you encounter the OVER_QUERY_LIMIT error again, try increasing the delay value.
  • Be mindful of the daily limit on the total number of geocoding requests as well.

The above is the detailed content of How to Avoid OVER_QUERY_LIMIT Errors in Google Maps API v3 When Geocoding Multiple Locations?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn