Translate this page:


Google Maps API Tutorial

© 2006, 2007, 2008, 2009 Mike Williams


Geocoding multiple addresses

If you've got lots of addresses to geocode, then you can't just write a loop that calls the getAddress() function in the previous example several times. There are several problems:
  • Geocoding takes quite a bit of computer power. Let's try to avoid needlessly wasting Google server resources.
  • If you send geocode requests to quickly, some of the requests will fail with code 620 "G_GEO_TOO_MANY_QUERIES".
  • Geocoding your locations each time will make your page take longer to open. A geocode request can sometimes take as long as a second to be processed.
  • There are awkward pitfalls for the unwary coder when there's more than one geocode request in flight at the same time.
  • Geocoders aren't too smart. If the Google geocoder can't find a good match for your address it will sometimes make guesses that a human would consider to be really silly. The geocoding database changes fairly rapidly, and it's possible that an address that it guesses right today might go wrong next week. If you geocode once and check the results then you know that you've got the right place.
If your webhost supports server-side scripting, then I recommend finding a non-Google batch geocoder and using that. If not, you can do something like this example, calling the Google geocoder as each reply comes back and displaying the results in a format that you can copy and paste into an XML file.

The goeocoder quota is now set at 15,000 geocode calls per IP address per day. The old 1.725 second speed limit per API key no longer applies, but there's a mysterious "limit to the maximum rate" mentioned here and here. At the time of writing, it seems that a 100ms delay between geocode requests works well for long runs. Shorter delays case more 620 errors and may take longer to complete the retries.

If you use .getLatLng() for geocoding multiple addresses, then you won't be able to recognise the 620 errors. I recommend using .getLocations() so that you can monitor the 620 errors. If you decide to use .getlatLng(), then I suggest using a considerably longer delay.

In my example, I start with a 100ms delay and increase the length of the delay each time I retry an address due to receiving a 620 error.

In my example, the list of target addresses is hard coded, but you could read the list from one XML file, pass forward all the attributes to your getAddress() function and include all the details in your output.

If you can run server-side scripting on your webserver, then you could send each result to your server to be stored in your database, but in that case you'd probably find it easier to use a non-Google batch geocoder that you could call directly from your server.

Back to Mike's Homepage
Up to the start of this tutorial