While developing my new application which uses reverse geocoding from Google Maps API v3, I came across the problem of rate-limiting the queries. Google happily returns "OVER_QUERY_LIMIT" instead of the result if you fire the requests rapidly. Sadly, the API does not solve this (which is quite stupid as the people must come up with workarounds). Anyway, here is a simple rate-limiter which should work in most cases, just replace
withvar geocoderService = new google.maps.Geocoder();
var geocoderService = { geocoder : new google.maps.Geocoder(), queue : [], delay: 2000, // in milliseconds timer: null, geocode: function(request, callback) { this.queue.push([request, callback]); if (this.timer == null) { this.timer = setInterval(this.processQueue.bind(this), this.delay); } }, processQueue: function() { if (this.queue.length > 0) { var data = this.queue.splice(0, 1)[0]; var request = data[0]; var callback = data[1]; this.geocoder.geocode(request, callback); } else { clearInterval(this.timer); this.timer = null; } }, };
No comments:
Post a Comment