Additional questions on batch geocoding with the Google Maps API v3

I'm trying to find a good way to limit the speed at which I submit geocoding requests to the Google Maps API v3 geocoding service. I know that Javascript does not have any good wait or sleep , because its execution is so far single-threaded.

Each geocoding request is sent inside the jQuery each function. So, the general code skeleton:

 $(xml).find('foo').each(function(){ // do stuff ... geocoder.geocode(request, function(results, status) {/* do other stuff */}); // do more stuff ... } 

How to set a fixed waiting interval between each call on geocode ? If I send each request as fast as Javascript will work, I will quickly start receiving OVER_QUERY_LIMIT responses, even if I send only 20 requests. This is expected, and I try to make my client play well with Google.


An alternative route that I am ready to continue is to completely abandon Javascript for geocoding and write it in Java. With Java, it would be very easy to sleep between requests.

However, I could not find a way to use the Google geocoding service (in particular, using version 3 of the API) in Java. GeoGoogle seems to be out of date for over a year, and uses v2.

Can this be done in Java, and if so, how?

+4
source share
4 answers

It’s best to implement some kind of speed limitation restrictions β€” use a timer object and a queue. The timer is scheduled at a fixed speed to work indefinitely (jQuery has some very good timer implementations), and in the body of this timer you push something out of the queue and send it and then end it. Then you add another code to this queue.

+2
source

Google is actively developing the v2 and v3 Maps APIs separately. v3 is a much thinner version, suitable for devices with limited processing power or basic mapping tasks (and the API key is not required for operation).

Anyway, if you are geocoding a lot of things, you should probably take a look at using the Google HTTP geocoding service ( http://code.google.com/apis/maps/documentation/geocoding/index.html ). This avoids the problem of speed limits.

And fwiw: Javascript can do basic sleep / wait operations with setInterval () and setTimeout (). Just run the request and reset the timer to do it again at the end of the callback processing.

+4
source

Here is a snippet from my script, basically it tries again if it removes google.maps.GeocoderStatus.OVER_QUERY_LIMIT

 function(results, status) { // If the limit is reached try again in a second if (status == google.maps.GeocoderStatus.OVER_QUERY_LIMIT) { window.setTimeout( function() {self.resolveAddress(addr);},1000 ); } else if (status == google.maps.GeocoderStatus.OK) { // Do something clever } else if (status == google.maps.GeocoderStatus.ZERO_RESULTS) { self.reduce(addr); // Try removing something from addr } else { atira.log('Something terrible happened'); } }); 
+1
source

All Articles