Geocoding with the Google Maps API Geocoder

When Google first released the highly popular Google Maps API, no geocoder was available. That was a major bummer, as giving customers the ability to find themselves on the map via address is probably the #1 most common thing a GIS developer needs to do, particularly for local government applications.

In June 2006, however, Google released its geocoder API. It has some advantages over Yahoo’s free geocoder in terms of the data output, but it’s main advantage is it allows 50,000 geocodes per day over Yahoo’s 5,000.

Google’s geocoder is a REST web service. If you were to see it as a URL, it would look something like this:

http://maps.google.com/maps/geo?q=
5501+Ruth+Drive+Charlotte+NC&output=xml&key=your_api_key


The return looks like this:
<?xml version=”1.0” encoding=”UTF-8” ?>
<kml xmlns=”http://earth.google.com/kml/2.0">
<Response>
<name>5501 Ruth Drive Charlotte NC</name>
<Status>
<code>200</code>
<request>geocode</request>
</Status>
<Placemark>
<address>5501 Ruth Dr, Charlotte, NC 28215, USA</address>
<AddressDetails Accuracy=”8” xmlns=”urn:oasis:names:tc:ciq:xsdschema:xAL:2.0”>
<Country>
<CountryNameCode>US</CountryNameCode>
<AdministrativeArea>
<AdministrativeAreaName>NC</AdministrativeAreaName>
<SubAdministrativeArea>
<SubAdministrativeAreaName>Mecklenburg</SubAdministrativeAreaName>
<Locality>
<LocalityName>Charlotte</LocalityName>
<Thoroughfare>
<ThoroughfareName>5501 Ruth Dr</ThoroughfareName>
</Thoroughfare>
<PostalCode>
<PostalCodeNumber>28215</PostalCodeNumber>
</PostalCode>
</Locality>
</SubAdministrativeArea>
</AdministrativeArea>
</Country>
</AddressDetails>
<Point>
<coordinates>-80.759577,35.248394,0</coordinates>
</Point>
</Placemark>
</Response>
</kml>

Everything a GIS developer needs to know about an address. Note the code - 200 means everything worked out peachy. There are a number of other codes you can view in Google’s api documentation, but they all amount to versions of the same thing - no match for you.

Accessing the Google geocoder, like any REST web service, is a piece of cake once you know the parameters. Here is some example PHP code (note I’m trying to be proper and use the CURL library here, so you’ll need to enable the php_curl extension either by dynamically loading it or through your php.ini file):
# Set a variable to your api key and the address you want to find.
$api_key = ‘your_api_key’;
$address = urlencode(‘5501 Ruth Drive, Charlotte NC’);

# Use the CURL library to access the Google geocoder and get a response
$googlemaps = curl_init();
curl_setopt($googlemaps, CURLOPT_HEADER, 0);
curl_setopt($googlemaps, CURLOPT_RETURNTRANSFER, 1);
$url = “http://maps.google.com/maps/geo?q=".$address.
“&output=xml&amp;amp;amp;amp;key=”.$api_key;
curl_setopt($googlemaps, CURLOPT_URL, $url);
$response = curl_exec($googlemaps);
curl_close($googlemaps);

# Load the response into a XML handler
$googleresult = simplexml_load_string($response);

# Process the return XML
if ($googleresult->Response->Status->code = 200) echo(‘We have a hit!‘);
else echo(‘Crikey! Does nobody type in their address correctly anymore?!‘);
That’s all there is to it. You can use this as a primary geocoding source or, as we do, as a backup option should geocoding to your own data fail to score a hit.

You have to register with Google maps for a key to use their service. It’s completely free and you can do it at http://www.google.com/apis/maps/ .