Created
April 11, 2013 16:40
-
-
Save micah1701/5364997 to your computer and use it in GitHub Desktop.
Use the Google Maps API to geocode a given address.
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 | |
$url = "https://maps.googleapis.com/maps/api/geocode/xml?sensor=false&address=" . urlencode($_GET['addr']); | |
$ch = curl_init(); | |
curl_setopt($ch, CURLOPT_URL, $url); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); | |
$raw_xml = curl_exec($ch); | |
curl_close($ch); | |
$xml = new SimpleXMLElement($raw_xml); | |
if(is_object($xml->result->geometry->location) ) | |
{ | |
echo json_encode( array('lat' => (string)$xml->result->geometry->location->lat, 'lng' => (string)$xml->result->geometry->location->lng)); | |
} | |
else | |
{ | |
echo json_encode(false); | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment