« Back to blog

Beating Google Maps' Geocoding Limits (sort of)

I've recently done some work to programmatically plot a number of markers on a Google Map based on normal street addresses.

Google Maps' Geocoding API does a great job of converting street addresses into Latitude and Longitude coordinates to enable you to plot the markers on the map.

The initial implementation of the map went well, plotting 2 or 3 markers without issue. Problems started surfacing however when we started putting 26 markers through the Geocoder at once - it bailed out after retrieving the coordinates of just 7 addresses. After a bit of research, it appears the Geocoder has some strict limits that don't like a lot of calls per second.

My initial code involved loading in all the required addresses into hidden <div>, which could be looped over using jQuery's .each method. The .html of each <div> could then be fed into the Geocoder to return the Latitudes and Longitudes.

With the addition of setTimeout against each call to the Geocoder API, we can reduce per-second requests and keep Google sweet!

$(".address").each(function () {


var address = $(this).children(".locationAddress").html();


setTimeout(function () {


geocoder1.geocode({ 'address': address }, function (results, status) {


if (i < $(".address").length) {
i++
}


if (status == google.maps.GeocoderStatus.OK) {
var marker = new google.maps.Marker({
map: googleMap,
position: results[0].geometry.location
});


}
});
}, 1000*i);
});
 

For my implementation, the adding of the markers to the map visibly a marker per second didn't matter (in fact, it actually enhanced it!) - but you might want to set up some kind of "Please wait" message whilst waiting for the markers to be retrieved.

 

Comments (2)

Jan 09, 2012
Chris said...
An alternative is converting the points to long lat when they are added. There is then no conversion from address to do at render time, so you can render a 1000 points without any problems.
Jan 09, 2012
Paul Gordon said...
But if you have no way of storing the points to be used when the page is rendered, you will still need to convert addresses on the fly, no?

Leave a comment...