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: Prev  1  2  

Performing Bulk Geocoding

As mentioned this methodology's greatest advantage is the ease in which it can help geocode large numbers of addresses. For example, suppose you wanted to create a map which charted the locations of area hospitals. For the purposes of this exercise I'll choose from five Columbus, OH-based hospitals, including The Ohio State University Medical Center, Children's Hospital, Grant Medical Center, Riverside Methodist Hospital, and Mount Carmel East. The hospital names and their corresponding addresses are stored in a MySQL table which might look like this:

CREATE TABLE hospitals (
   id SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
   name VARCHAR(75) NOT NULL,
   address VARCHAR(100) NOT NULL,
   city VARCHAR(50) NOT NULL,
   state CHAR(2) NOT NULL,
   zip CHAR(5) NOT NULL,
   latitude FLOAT(8,6),
   longitude FLOAT(8,6)
)

The following script will retrieve the address information found in this table, encoding it for transmission via HTTP, and output the appropriate latitudinal and longitudinal coordinates:

<?php

   // Your Google Maps API key
   $key = "YOUR_KEY_HERE";

   // Connect to the MySQL database
   $conn = mysql_connect("localhost", "jason", "secretpswd");

   // Select the database
   $db = mysql_select_db("googlemaps");

   // Query the table
   $query = "SELECT id, address, city, state FROM hospitals";
   $result = mysql_query($query) or die(mysql_error());

   // Loop through each row, submit HTTP request, output coordinates
   while (list($id, $address, $city, $state) = mysql_fetch_row($result))
   {
      $mapaddress = urlencode("$address $city $state");

      // Desired address
      $url = "http://maps.google.com/maps/geo?q=$mapaddress&output=xml&key=$key";

      // Retrieve the URL contents
      $page = file_get_contents($url);

      // Parse the returned XML file
      $xml = new SimpleXMLElement($page);

      // Parse the coordinate string
      list($longitude, $latitude, $altitude) = explode(",", $xml->Response->Placemark->Point->coordinates);

      // Output the coordinates
      echo "latitude: $latitude, longitude: $longitude <br />";

   }

?>
Executing this script produces the following output:
latitude: 39.993932, longitude: -83.018649
latitude: 39.952297, longitude: -82.978954
latitude: 39.960770, longitude: -82.990525
latitude: 40.031565, longitude: -83.031058
latitude: 39.957899, longitude: -83.019699

Of course, this script can be easily modified to insert the data into the appropriate row.

Conclusion

I'll continue expanding this series as Google evolves it's mapping API. In the meantime, I invite you to email me with URLs to your Google Maps mashups. Also, please feel free to email me with specific API features you'd like me to cover in future articles!

About the Author

W. Jason Gilmore has developed countless Web applications over the past seven years and has dozens of articles to his credit on Internet application development topics. He is the author of three books, including most recently "Beginning PHP 5 and MySQL 5: From Novice to Professional", (Apress), now in its second edition, and with co-author Robert Treat, "Beginning PHP and PostgreSQL 8: From Novice to Professional" (Apress). Jason is Apress' Open Source Editorial Director, and is the co-founder of IT Enlightenment, a technical training company. Check out his blog at http://www.wjgilmore.com/.

Go to page: Prev  1  2  


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