Beating Google Maps' Geocoding Limits (sort of)
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.
