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

2

7

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
Lodie answered 4/10, 2011 at 4:25 Comment(0)
B
8

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.

Barhorst answered 4/10, 2011 at 7:22 Comment(4)
I think there is also a limit of 10 requests per second, which is probably the limit that is being hit here.Altruistic
The limit for a daily cap of 2500 is one every 35 seconds.Lavernelaverock
This solved my problem perfectly, thanks! I mistakenly assumed there was only a daily limit. In case anyone was wondering, a 1 second delay worked with no problems, so it definitely allows a rate higher than 1/sec.Lodie
Paul is correct according to this Google FAQ page: "There is also a rate limit of 10 requests per second that applies to all Google Maps API Web Services." - code.google.com/apis/maps/documentation/premier/…Schilit
P
2

I have co me to realize that most peope (like me) omit the part in the google map api that talks about the 'request rate limit' which is different then the 'page per day limit'.

so the 'request rate limit ' is of 10 requests per seconds.

So based on that if you display a page with 20 static maps... and the end user has a fast internet connection (maybe you?)...

then those 20 requests to the google map API happen faster then in the lapse of 1 second...

so by this you break their ryle and they block the image display.

this is my conclusion after just now reading on it here and there

good luck

Pukka answered 21/11, 2011 at 4:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.