Created
November 19, 2013 04:10
-
-
Save robynitp/7540199 to your computer and use it in GitHub Desktop.
Example of accessing the Google Geocoding API and getting results in XML
Info here: https://developers.google.com/maps/documentation/geocoding/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Example of accessing the Google Geocoding API and getting results in XML | |
Info here: https://developers.google.com/maps/documentation/geocoding/ | |
*/ | |
$base_url = 'http://maps.googleapis.com/maps/api/geocode/'; | |
$format = 'xml'; // 'xml' or 'json' | |
$address = 'address='.urlencode('350 5th Avenue New York, NY'); // makes the text URL friendly, ie, 350+5th+Avenue+New+York%2C+NY | |
$url = $base_url.$format.'?'.$address.'&sensor=false'; // Google requires 'sensor=false' parameter | |
// url should look something like: http://maps.googleapis.com/maps/api/geocode/xml?address=350+5th+Avenue+New+York%2C+NY&sensor=false | |
// Get the document at this url | |
$response = file_get_contents($url); | |
// echo $response; //debug | |
// Parse XML with SimpleXML in PHP http://www.php.net/manual/en/simplexml.examples-basic.php | |
$xml = new SimpleXMLElement($response); | |
//var_dump($xml); // take a look at this to see what data you get back | |
// For example, you could show the latitude and longitude coordinates | |
echo "Latitude: ".$xml->result->geometry->location->lat."<br/>\n"; | |
echo "Longitude: ".$xml->result->geometry->location->lng; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
REQUEST_DENIED