Home  >  Article  >  Web Front-end  >  How to Prevent the Google Maps API OVER_QUERY_LIMIT Error in v3?

How to Prevent the Google Maps API OVER_QUERY_LIMIT Error in v3?

Patricia Arquette
Patricia ArquetteOriginal
2024-11-02 22:50:02319browse

How to Prevent the Google Maps API OVER_QUERY_LIMIT Error in v3?

How to Handle OVER_QUERY_LIMIT Error in Google Maps API v3

Introduction:

In Google Maps API v3, you may encounter the OVER_QUERY_LIMIT error when making too many geocoding requests in a short period. To avoid this error, it's necessary to implement a delay mechanism between requests.

JavaScript Implementation:

To introduce a pause between geocoding calls, you can use the following JavaScript code:

<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>

This code checks for existing delays, sets a new delay if the OVER_QUERY_LIMIT error occurs, and waits for the delay to finish before resuming geocoding.

Example:

In your provided code, you can replace the existing codeAddress function with the updated version:

<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>

This modification will introduce the necessary delays to prevent the OVER_QUERY_LIMIT error.

The above is the detailed content of How to Prevent the Google Maps API OVER_QUERY_LIMIT Error in v3?. 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