Validate and Format Addresses using Google API

xster
xster
Published in
1 min readApr 21, 2011

Suppose you want to make some web app that lets users input addresses. I would be nice to

  1. Weed out minor misspelling
  2. Standardise format (proper capitalisation, abbreviated province/state or full name etc)
  3. Input in freeform but store civic number, street name, city etc separately
  4. Verify the address exists of course

Some governmental entities provide this information via public APIs but if you want a uniform service, why not use Google?

Fortunately, Thor Mitchell, a Google Product Manager, posted an excellent example code that does all of the above using Google Maps JavaScript API. Even better, it’s with the new V3 so it’s not obsolete anytime soon. It’s originally posted here in the comments of a blog by Gabe Sumner. It definitely deserves more recognition so here it is again as is:

< !DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">




Google Maps JavaScript API v3 Example: Map Simple
AddressValid: Matched: Result:

Only 100 lines! It uses Google’s GeoCoding JavaScript API and you can fancy it up easily with jQuery. In the validate function, the results variable contains of course json formatted responses as described in Google’s docs. Looking at ‘address_components’ will give you the properly formatted address separated by parts which you can put into database.

For a backend solution, you can use pygeocoder, a Python library, to validate addresses.

--

--