views:

3578

answers:

4

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?

+2  A: 

Google is actively developing v2 and v3 of the Maps API separately. v3 is a much slimmer version suitable for devices with limited processing power or basic mapping tasks (and doesn't need an API key to work).

In either case, if you're geocoding many things you should probably look at using Google's HTTP geocoding service (http://code.google.com/apis/maps/documentation/geocoding/index.html). This will avoid the rate limiting issue.

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

Ty W
Sorry, but that's really useless to me. The HTTP geocoding service you linked is specific to v2 *and* is subject to the same rate limits and daily limits: http://code.google.com/apis/maps/documentation/geocoding/index.html#StatusCodes. Javascript's setTimeout, in fact, does not suffice for this because it just schedules a function to run X milliseconds in the future - I still can't really control the amount of time in between executions of that function.
Matt Ball
setTimeout and setInterval are the basic javascript calls used to make any javascript timer... so in fact it is exactly the answer that you marked as accepted. At any rate, having to rate limit your geocode requests indicates poor app design, usually the result of not saving previous geocode responses or having something better suited to http batch geocoding using a non-google service.
Ty W
I will be saving previous geocoding responses. However, (a) I have about 10,000 addresses that I need to get initial locations for and (b) some of those addresses change daily. If I have to look up even 20 of those new addresses, I *have* to limit the rate at which I send requests for them. As for an HTTP batch geocoding service - I couldn't find any that were as good as Google's in terms of price (free), hight daily request limit, and accuracy of data - particularly one that has a Javascript or Java API. Can you point me in the direction of one?
Matt Ball
the google map api group maintains a page of non-google geocoders: http://groups.google.com/group/google-maps-api/web/resources-non-google-geocoders. without knowing anything about your application I can't say it'd help or not, but remember that javascript geocoder requests are throttled based on the client's IP not your server's, so you may be able to take advantage of that somehow.
Ty W
+1  A: 

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.

Chris Thompson
That sounds way better than trying to restructure my code so that the requests are all done server-side. Are timers built into jquery, or added in as plugins? Can you recommend a specific one?
Matt Ball
The one I've had really good experiences with is a plugin and can be found at http://plugins.jquery.com/project/timers.Good luck!
Chris Thompson
A: 

Hey even i am facing the same problem when i send requests more than 10 at a time, so can some one suggest me wat to do, i even tried the setInterval(), etc which are available in javascript. it would be really helpful if some one can give me suggestions as i am running out of time :(

Sriharsha
`setInterval()` is the simplest way to do it. If you're having problems with the implementation, I suggest that you open a new question with your specific problems.
Matt Ball