"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > How to Avoid OVER_QUERY_LIMIT Errors in Google Maps API v3?

How to Avoid OVER_QUERY_LIMIT Errors in Google Maps API v3?

Published on 2024-11-12
Browse:341

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:

function codeAddress(vPostCode) {
    if (geocoder) {
        setTimeout(function() {
            geocoder.geocode({ 'address': "'"   vPostCode   "'"}, function(results, status) {
                // Code for handling the geocoding result
            });
        }, 2000);
    }
}

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:

  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();
      }
    );
  }

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.

Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3