More batch geocoding questions with the Google Maps API v3

geocoding, google-maps-api-3, java, javascript, jquery

Solution

Your best bet to implement some sort of rate-limited submissions would be to use a timer object and a queue. The timer is scheduled at a fixed rate to run indefinitely (jQuery has some very nice timer implementations) and in the body of that timer, you pop something off the queue and submit it and then finish. You other code then adds things to that queue as needed.

Problem

I'm trying to figure out a nice way to limit the rate at which I send geocode requests to the Google Maps API v3 geocoder service. I know that Javascript does not have any nice `wait` or `sleep` because its execution is, for the time being, single-threaded. Each geocoder request is sent inside of a jQuery `each` function. So, the general code skeleton is: ``` $(xml).find('foo').each(function(){ // do stuff ... geocoder.geocode(request, function(results, status) {/* do other stuff */}); // do more stuff ... } ``` How can I set a fixed interval to wait in between each call to `geocode`? If I send each request as fast as Javascript will run, then I quickly start receiving `OVER_QUERY_LIMIT` responses - even if I'm only sending 20 requests. This is expected, and I'm trying to make my client play nicely with Google's service. An alternate route I'm willing to pursue is to completely abandon Javascript for geocoding, and write it all in Java. With Java it would be really easy to sleep in between requests. However, I couldn't find a way to use Google's geocoding service (specifically, using version 3 of the API) in Java. GeoGoogle seems to be more than a year out of date, and uses v2. Can it be done in Java, and if so, how?

Original source