Home >Web Front-end >JS Tutorial >How to Avoid OVER_QUERY_LIMIT Errors in Google Maps API v3 with Javascript?

How to Avoid OVER_QUERY_LIMIT Errors in Google Maps API v3 with Javascript?

Linda Hamilton
Linda HamiltonOriginal
2024-11-02 16:30:02572browse

How to Avoid OVER_QUERY_LIMIT Errors in Google Maps API v3 with Javascript?

Determining OVER_QUERY_LIMIT in Google Maps API v3: Slowing Down Requests with Javascript

The OVER_QUERY_LIMIT error frequently occurs when making excessive geocoding requests in Google Maps API v3. To avoid this error, it's essential to introduce a pause between each request to slow down the process.

Here's the original code:

<code class="javascript">function codeAddress(vPostCode) {
  if (geocoder) {
    geocoder.geocode( { 'address': "'" + vPostCode + "'"}, function(results, status) {
      if (status == google.maps.GeocoderStatus.OK) {
        // Handling success
      } else if (status == google.maps.GeocoderStatus.OVER_QUERY_LIMIT) { 
        // Handling OVER_QUERY_LIMIT error
      } else {
        // Handling other errors
      }
    });
  }
}</code>

To implement the pause, we can utilize the following code:

<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) {
        // Handling success
      } else if (status == google.maps.GeocoderStatus.OVER_QUERY_LIMIT) { 
        wait = true;
        setTimeout("wait = true", 2000);
        // Handling OVER_QUERY_LIMIT error
      } else {
        // Handling other errors
      }
    });
  }
}</code>

In this modified code, we introduce a wait variable to pause the execution when the OVER_QUERY_LIMIT error occurs. A timeout of 2000 milliseconds is then set to resume the execution, providing a delay between requests.

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