Google maps – geocoder.getLatLng limitation problem

With one of my recent projects I find out limitations on calling Google maps geocoder functions. Google prevents huge amount of requests. They allow only certain amount of requests per one time period (I was able to make 10 requests at once). To find out what was a problem and also fixing this task takes me a while, maybe this post save you some debugging (google-ing).

1. Fast and simple solution

Use JavaScript window.setTimeout for each calling geocoder functions. 180 ms delay between two requests was enough time for me.


var map = new GMap2( document.getElementById( "map" ) );
map.addControl( new GSmallMapControl() );

// some addresses for demonstration..
>var addressArray = [];
addressArray.push( "Kralovoholska 1, Banska Bystrica, SK" );
addressArray.push( "Magurska 1, Banska Bystrica, SK" );
addressArray.push( "Krivanska 1, Banska Bystrica, SK" );
addressArray.push( "Karpatska 1, Banska Bystrica, SK" );
addressArray.push( "Inovecka 1, Banska Bystrica, SK" );
addressArray.push( "Javornicka 1, Banska Bystrica, SK" );
addressArray.push( "Rudohorska 1, Banska Bystrica, SK" );
addressArray.push( "Starohorska 1, Banska Bystrica, SK" );
addressArray.push( "Sitnianska 1, Banska Bystrica, SK" );
addressArray.push( "Strazovska 1, Banska Bystrica, SK" );
addressArray.push( "Tatranska 1, Banska Bystrica, SK" );
addressArray.push( "Pieninska 1, Banska Bystrica, SK" );
addressArray.push( "Dumbierska 1, Banska Bystrica, SK" );

// add markers in a loop
>for( var i in addressArray ) {
// delay for each marker
timeout = parseInt( i ) * 180;
window.setTimeout( function() {
geocoder.getLatLng( addressArray.pop(), function(point) {

if (point) { map.addOverlay( new GMarker(point)); }
});
}, timeout);
}

More sophisticated solution can be created by using Ajax (when you get latitude and longitude save it in DB or file system). It will be faster to get geo – locations data from DB than from Google geocoder requests..


This entry was posted in Professional, Wordpress and tagged , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>