Edmonton Web Design and Photography

Google HTTP Geocoding


Posted in: Tutorials on June 22nd, 2007

Google Maps API I have been doing a lot of searching in regards to Google Maps and information on there API. In my Addressbook I had a some what dynamic but clumsy way of showing some ones location.

So when I would pull the information I would use a PHP function called “urlencode” it basically converts spaces to underscores so your URL won’t explode and make Google angry.

This works fine but I was opening a new window that I had no control over. so that’s when I began to look into the API and after a bit of looking I found that they had support for HTTP Request and by doing so Google spits out a bit of XML.

The basic URL looks like this:

http://maps.google.com/maps/geo?q=address,+city,+state&output=xml&key=abcdefg

Using + to replace spaces and, to signal the next field.

Click to see output example

There were a few options for output “xml, kml, csv, or json” and after a bit of research i chose XML. Something i haven’t had a lot of experience to work with.

In my application I have all the addresses saved in a Database and need to save the Latitude and Longitude so using the same method as my past example i used the “urlencode” function to make my addresses safe for the URL.

   $googleAddress = "http://maps.google.com/maps/geo?q=".urlencode($address) .'+'. urlencode($city) .'+'. urlencode($province).'+'. urlencode($country)."&output=xml&key=$key";

The next task was to get the contents of the file so I used the “file_get_contents” function and passed it my URL

   $googlePage = file_get_contents($googleAddress);

This is where I had some learning to do, after the XML was opened I had to gather the properties. I used the “SimpleXMLElement” function to convert XML to an object that can be processed with normal property selector.

Note: The SimpleXMLElement will only work in PHP 5+

$xml = new SimpleXMLElement($googlePage);

Now that we have the values from the XML we can assign variables to them that will allow us to display the results.

http://ca.php.net/xml

list($lng, $lat, $altitude) = explode(",",
     $xml->Response->Placemark->Point->coordinates);

All that is left to do is display the results, at this point you could also use the information and store it in a database as i did for my Addressbook.

echo "Longitude: $longitude, Latitude: $latitude";

Full Code:


   // API Key
   $key=""

   // Google Geo Address
   $googleAddress = "http://maps.google.com/maps/geo?q=".urlencode($address) .'+'. urlencode($city) .'+'. urlencode($province).'+'. urlencode($country)."&output=xml&key=$key";

   // Retrieve the URL contents
   $googlePage = file_get_contents($googleAddress);

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

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

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

That’s about it on this subject; there are many ways to Geocode and this is just one of them.

Links:
Google Geocoding HTTP Request

7 Responses to “ Google HTTP Geocoding ”


  1. You might want to remove your maps API key from your blog post, as I don’t think you’re supposed to be sharing that with the public =)

    Dan
    June 29, 2007

  2. What API key :)

    Thanks.

    Adam Patterson
    June 29, 2007

  3. [...] Studio Lounge: Web Design and Photography Edmonton » Google HTTP Geocoding (tags: google geocoding api googlemaps) [...]

    links for 2007-06-30
    June 30, 2007

  4. [...] Google HTTP geocoding – разводим гугл на координаты меÑ?тноÑ?ти [...]


  5. No need to hide your API key. You can only use it in the domain to entered when you requested it.

    cubus
    July 19, 2007

  6. This is true :)

    Adam Patterson
    July 19, 2007

  7. [...] I have talked about simplexml_load_string() in Google HTTP Geocoding. [...]


Search


Archive


Subscribe