Performing HTTP Geocoding with the Google Maps API
In this article I'll show you how to use the HTTP request method in conjunction with PHP, culminating with an example involving the geocoding of numerous addresses for storage in a MySQL database.
A Simple Example
Let's begin with using the HTTP method to retrieve the coordinates of the greatest college football stadium in the United States: The Horseshoe. Because this request (easily done using PHP's file_get_contents() function) returns an XML document, I'll filter the output through htmlspecialchars() so the document can be properly output to the screen. The code is found below, followed by the output:
<?php // Your Google Maps API key $key = "YOUR_KEY_HERE"; // Desired address $address = "http://maps.google.com/maps/geo?q=411+Woody+Hayes+Drive,+Columbus,+OH&output=xml&key=$key"; // Retrieve the URL contents echo htmlspecialchars(file_get_contents($address)); ?>
Executing this script produces the following output (formatted for this article):
<?xml version="1.0" encoding="UTF-8"?> <kml xmlns="http://earth.google.com/kml/2.0"> <Response> <name>411 Woody Hayes Drive, Columbus, OH</name> <Status> <code>200</code> <request>geocode</request> </Status> <Placemark> <address>411 Woody Hayes Dr, Columbus, OH 43210, USA</address> <AddressDetails xmlns="urn:oasis:names:tc:ciq:xsdschema:xAL:2.0"> <Country> <CountryNameCode>US</CountryNameCode> <AdministrativeArea> <AdministrativeAreaName>OH</AdministrativeAreaName> <SubAdministrativeArea> <SubAdministrativeAreaName>Franklin</SubAdministrativeAreaName> <Locality> <LocalityName>Columbus</LocalityName> <Thoroughfare> <ThoroughfareName>411 Woody Hayes Dr</ThoroughfareName> </Thoroughfare> <PostalCode> <PostalCodeNumber>43210</PostalCodeNumber> </PostalCode> </Locality> </SubAdministrativeArea> </AdministrativeArea> </Country> </AddressDetails> <Point><coordinates>-83.019739,40.004098,0</coordinates></Point> </Placemark> </Response> </kml>
Note the coordinates are embedded towards the bottom of the document. All we need to do is parse the document to retrieve this information. This is much easier than it seems, as you'll soon learn.
You might be wondering why the document's root element is <kml> rather than <xml>. KML is an XML grammer called Keyhole Markup Language, which is used to describe the geographic data found in Google Earth. If you save this as a file (with a .kml extension) you can drag it onto the Google Earth 3D viewer to see the visual representation. While KML is the default, you also have the option of receiving this data in XML markup, or a JSON object, done by appending &output=[xml|json] to the URL. If your interest lies solely in retrieving the coordinates, KML will work just fine as you'll soon see. However if you have reason to use one of the other formats, by all means adjust the output parameter accordingly. Keep in mind that the returned XML and KML documents are identical except for the MIME type.
Of course, it's likely the only bit of information you're interested in is the coordinates. You'll find this information in the Response » Placemark » Point » coordinates node, which includes the appropriate longitude, latitude and altitude values (at present altitude is always set to zero). Thankfully, PHP's SimpleXML extension makes retrieving these coordinates from the returned XML string a trivial affair. Revising the previous example, the returned data is parsed with SimpleXML, and the coordinates output:
<?php // Your Google Maps API key $key = "YOUR_KEY_HERE"; // Desired address $address = "http://maps.google.com/maps/geo?q=411+Woody+Hayes+Drive,+Columbus,+OH&output=xml&key=$key"; // Retrieve the URL contents $page = file_get_contents($address); // Parse the returned XML file $xml = new SimpleXMLElement($page); // Retrieve the desired XML node echo $xml->Response->Placemark->Point->coordinates; ?>
Executing this script returns the following output: -83.019739,40.004098,0. Therefore we'll need to the previous script to parse this data, replacing the echo statement with the following:
// Parse the coordinate string list($longitude, $latitude, $altitude) = explode(",", $xml->Response->Placemark->Point->coordinates); // Output the coordinates echo "Longitude: $longitude, Latitude: $latitude";
Executing the revised script produces:
Longitude: -83.019739, Latitude: 40.004098
Page 1 of 2