I am performing distance queries using the google api
for r in cursorConsulta:
googleGeocodeUrl = 'https://maps.googleapis.com/maps/api/place/nearbysearch/json?location='
punto = str(r[1])+','+str(r[0])
id = r[2]
#radioBusqueda = r[3]
radio = '&radius=4000'
clave = '&keyword=' + 'anses'
APIKEY = '&key=' + '****'
url = googleGeocodeUrl + punto + radio + clave + APIKEY
print(url)
json_response = urllib.request.urlopen(url)
busqueda = json_response.read().decode('utf-8')
busquedajson = json.loads(busqueda)
result = busquedajson['status']
When the api returns the status
OVER_QUERY_LIMIT
Generate a new api key to be able to process again
if result=="OVER_QUERY_LIMIT":
googleGeocodeUrl = 'https://maps.googleapis.com/maps/api/place/nearbysearch/json?location='
punto = str(r[1])+','+str(r[0])
id = r[2]
#radioBusqueda = r[3]
radio = '&radius=4000'
clave = '&keyword=' + 'anses'
APIKEY = '&key=' + '****'
url = googleGeocodeUrl + punto + radio + clave + APIKEY
print(url)
json_response = urllib.request.urlopen(url)
busqueda = json_response.read().decode('utf-8')
busquedajson = json.loads(busqueda)
The question is as follows, enter the first api query and reprocess the allowed amount, when it reaches its maximum enter the if result=="OVER_QUERY_LIMIT": and process again, now when it generates the OVER_QUERY_LIMIT message again the second API KEY as I can do so that the application has already that if it does not continue executing with the message of exceeded queries.
It's been a while since this publication was made, but I will make this answer with the aim of helping those who enter in search of an accurate answer.
My environment is Ionic 3, although the idea can be brought to any environment.
I had the same problem with the Google API using
DistanceMatrixService
, which measures the distance between two coordinates, the problem arose when performing an iteration on aArray
in which there was a query to the aforementioned API, this resulted in an error.I solved this with the use of a
timer
, which every certain period of time (tiny period) makes a query to the Google API and thus not exceed the limit of queries per time.I hope this is helpful for those looking for a solution to this problem.
Greetings, Leandro.