Hitting rate limit for google maps API, But don't know why

Kevin picture Kevin · Oct 4, 2011 · Viewed 8k times · Source

I've written a script to send an address to Google Maps' API and receive back the Lat and Lng. However, I'm receiving error messages that I've exceeded Google's rate limit after 20 or so queries. Is there something I'm not considering?

I'd appreciate any help. I'm very new at using API's so better understanding why I'm hitting the rate limit would be very helpful.

After reading the addresses from a csv file named Location, below is my relevant code.

    for row in locations:
        address = 'XXX, New Haven, CT'
        first = re.search('^(.*),',row[0])
        address = re.sub('XXX',first.group(), address)
        lat, lng = gmaps.address_to_latlng(address)

And my error message is below.

    Traceback (most recent call last):
    File "<input>", line 5, in <module>
    File "/usr/local/Cellar/python/2.7.2/lib/python2.7/site-packages/googlemaps-1.
    0.2-py2.7.egg/googlemaps.py", line 310, in address_to_latlng
        return tuple(self.geocode(address)['Placemark'][0]['Point']['coordinates'][1
    ::-1])
      File "/usr/local/Cellar/python/2.7.2/lib/python2.7/site-packages/googlemaps-1.
    0.2-py2.7.egg/googlemaps.py", line 262, in geocode
        raise GoogleMapsError(status_code, url, response)
    GoogleMapsError: Error 620: G_GEO_TOO_MANY_QUERIES

Answer

Petr Viktorin picture Petr Viktorin · Oct 4, 2011

Each gmaps.address_to_latlng call sends a request to the Google server, and you can only make a limited number of those.

Google's docs on usage limits:

Use of the Google Geocoding API is subject to a query limit of 2,500 geolocation requests per day. [...] Additionally, we enforce a request rate limit to prevent abuse of the service.

And the docs on G_GEO_TOO_MANY_QUERIES :

The given key has gone over the requests limit in the 24 hour period or has submitted too many requests in too short a period of time. If you're sending multiple requests in parallel or in a tight loop, use a timer or pause in your code to make sure you don't send the requests too quickly.

So, do just what they tell you to do:

import time

# And then in the loop, pause:
time.sleep(1)

Adjust the „1“ to an appropriate number of seconds so you don't run out of allowed requests.