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
Website Load Testing
Cell Phones
Televisions
KVM over IP
Computer Deals
Compare Prices
Corporate Awards
Dental Insurance
Logo Design
Boat Donations
Auto Insurance Quote
Web Hosting Directory
PDA Phones & Cases
Online Education

 


Download these IBM resources today!


Webcast: Hacking 101--The Top 10 Attacks in Web Applications
Learn about the three most common web application attacks, including how they occur and what can be done to prevent them.

eKit: Web Application Security
Discover how IBM Rational AppScan Standard Edition can help you detect vulnerabilities in your Web applications. The new Web Application Security eKit provides you with valuable resources, including whitepapers, demos, and additional information on the benefits of testing your Web applications.

Tutorial: Create Secure Java Applications Productively
This is the first in a two-part tutorial series creating secure Java-based Web applications using Rational Application Developer, Data Studio and Rational AppScan.

eKit: Web 2.0 Developer
Take advantage of open, flexible Web 2.0 technologies, like social software and mash-ups. The IBM Web 2.0 Developer eKit has been updated with the latest best practices & technologies from IBM.
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 -
iPhone Knocking on the Enterprise    July 14, 2008
Linux 2.6.26 Opens Up to Debugging    July 14, 2008
Borland Launches ALM Management Tools    July 14, 2008
Nominations Open for CEO Vision Awards    July 11, 2008
Free Tech Newsletter -

Q&A with Bob Muglia: Senior VP, Server and Tools Division. Learn how Microsoft's new interoperability principles and the steps the company is taking to increase the openness of its products.

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

Is it time to make your move to the multi-threaded and parallel processing world? Find out!
Generate Complete .NET Web Apps in Minutes . Download Iron Speed Designer today.
Intel Go Parallel Portal: Translating Multicore Power into Application Performance. Learn more.
Walkthrough: Building a Mobile Game in Visual Studio 2008
HP Whitepaper: Remote Replication Best Practices for Oracle10g Using XP Continuous Access



JupiterOnlineMedia

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info


Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers