November 27, 2020
Hot Topics:

Performing HTTP Geocoding with the Google Maps API

  • By Jason Gilmore
  • Send Email »
  • More Articles »

The previous installment of this ongoing series covering the Google Maps API (Part 1, Part 2, Part 3, Part 4, Part 5), I introduced the API's new geocoding feature, released this past June 11. In this tutorial I showed you how to submit geocoding requests via JavaScript, which is convenient because it's easy to subsequently build a map using the retrieved coordinates. However, if you're interested in performing bulk geocoding for reason of storing a series of coordinates in a database, you might be interested in taking advantage of a second means for geocoding, accomplished by way of HTTP request. Storing the coordinates locally will not only decrease the number of required daily geocoding requests (limited to 50,000 daily), but it will speed your application by cutting down on the total requests required to ultimately build a map.

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:


   // 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">
     <name>411 Woody Hayes Drive, Columbus, OH</name>
        <address>411 Woody Hayes Dr, Columbus, OH 43210, USA</address>
        <AddressDetails xmlns="urn:oasis:names:tc:ciq:xsdschema:xAL:2.0">
                          <ThoroughfareName>411 Woody Hayes Dr</ThoroughfareName>

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:


   // 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(",", 

// Output the coordinates
echo "Longitude: $longitude, Latitude: $latitude";

Executing the revised script produces:

Longitude: -83.019739, Latitude: 40.004098

Page 1 of 2

This article was originally published on July 24, 2006

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date