My goal is to geocode a large number of addresses into LatLng
objects.
I'm only allowed to use Google and I can't use other APIs that may support batch requests.
Therefore I have to go with 1 request = 1 address, but I keep getting OVER_QUERY_LIMIT
. Not sure how that is possible because, from official docs, I should be able to geocode 50 addresses per second, client-side.
My first thought was to make 50 requests at once, wait for all to return, pause a second, and continue with the next 50 addresses... So the simplified code looked something like this:
const geocoder = new google.maps.Geocoder();
const allAddresses = [ /* 500 addresses */ ];
geocodeAddresses(allAddresses);
function geocodeAddresses(addresses) {
const last50 = addresses.splice(-50);
Promise.allSettled(
last50.map((address) =>
new Promise((resolve, reject) =>
geocoder.geocode({
address
}, (results, status) => {
console.log("Geocoded! Response: " + status);
resolve();
})))
).then(() =>
setTimeout(() => geocodeAddresses(addresses), 1000)
);
}
This code successfully returns the first 10 requests at once and then fails on others due to quota limits (why?).
So I decided to go with 1 request at a time with 500ms delay after each result, which is already extremely slow. I'd then repeat OVER_QUERY_LIMIT requests with a 2000ms timeout. The result is very weird:
- first ~200 requests all went ok
- next ~50 requests: ~10 requests repeated
- next ~30 requests: ~15 requests repeated
- next ~10 requests: ~10 requests repeated (each failed before success)
and after ~350 completed requests, each failed 5-6 times and it was really slow. It seems like it may be failing exponentially.
What is the fastest way to geocode all my addresses with Google? Thanks!