developer.com
Search EarthWeb
CodeGuru | Gamelan | Jars | Wireless | Discussions
Navigate developer.com
Architecture & Design  
Database  
Java
Languages & Tools
Microsoft & .NET
Open Source  
Project Management  
Security  
Techniques  
Voice  
Web Services  
Wireless/Mobile
XML  
New
 
Technology Jobs  

   Developer.com Webcasts:
  The Impact of Coding Standards and Code Reviews

  Project Management for the Developer

  Defining Your Own Software Development Methodology

  more Webcasts...




See The Winners!




Developer Jobs

Be a Commerce Partner














 


Related Article -
Introducing Google's Geocoding Service
Building a Geocoding Web Service
Build Your Own Geocoding Solution with Geo::Coder::US
Retrieving Map Location Coordinates
Integrating Google Maps into Your Web Applications
Developer News -
First Major PHP Update in Years Coming Soon    June 25, 2009
Red Hat CEO Calls on Oracle to Keep Java Open    June 25, 2009
Google Widens AdSense for iPhone, Android Apps    June 24, 2009
Eclipse Galileo Releases 33 Open Source Projects    June 24, 2009
Free Tech Newsletter -

Performing HTTP Geocoding with the Google Maps API
By Jason Gilmore

Go to page: 1  2  Next  

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:

<?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

Go to page: 1  2  Next  


Tools:
Add www.developer.com to your favorites
Add www.developer.com to your browser search box
IE 7 | Firefox 2.0 | Firefox 1.5.x
Receive news via our XML/RSS feed


Database Archives






internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

Legal Notices, Licensing, Reprints, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs