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:
The return looks like this:
<?xml version=”1.0” encoding=”UTF-8” ?>
<name>5501 Ruth Drive Charlotte NC</name>
<address>5501 Ruth Dr, Charlotte, NC 28215, USA</address>
<AddressDetails Accuracy=”8” xmlns=”urn:oasis:names:tc:ciq:xsdschema:xAL:2.0”>
<ThoroughfareName>5501 Ruth Dr</ThoroughfareName>
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.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.
$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.
curl_setopt($googlemaps, CURLOPT_URL, $url);
$response = curl_exec($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?!‘);
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/ .