How to use Google HTTP Geocoding


/ Published in: PHP
Save to your folder(s)



Copy this code and paste it in your HTML
  1. $address = '1 Sir Winston Churchill Square';
  2. $city = 'edmonton';
  3. $province = 'alberta';
  4. $country = 'canada';
  5. $postalcode = 'T5J 2R7';
  6.  
  7. // Google Geo Address
  8. $googleAddress = "http://maps.google.com/maps/geo?q=".urlencode($address) .'+'. urlencode($city) .'+'. urlencode($province).'+'. urlencode($postalcode).'+'. urlencode($country)."&output=xml";
  9.  
  10. // Retrieve the URL contents
  11. $googlePage = file_get_contents($googleAddress);
  12.  
  13. // Parse the returned XML file
  14. $xml = new SimpleXMLElement($googlePage);
  15.  
  16. // Parse the coordinate string
  17. list($longitude, $latitude, $altitude) = explode(",", $xml->Response->Placemark->Point->coordinates);
  18.  
  19. // Output the coordinates
  20. echo "Longitude: $longitude, Latitude: $latitude";

URL: http://www.adampatterson.ca/blog/2010/09/google-http-geocoding/

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.