
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.
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.
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
June 29, 2007 at 3:25 pm
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 =)
June 29, 2007 at 4:31 pm
What API key
Thanks.
June 30, 2007 at 3:30 pm
[...] Studio Lounge: Web Design and Photography Edmonton » Google HTTP Geocoding (tags: google geocoding api googlemaps) [...]
July 1, 2007 at 10:27 am
[...] Google HTTP geocoding - разводим гугл на координаты меÑ?тноÑ?ти [...]
July 19, 2007 at 6:22 pm
No need to hide your API key. You can only use it in the domain to entered when you requested it.
July 19, 2007 at 10:30 pm
This is true