Skip to content

Instantly share code, notes, and snippets.

@micah1701
Created April 11, 2013 16:40
Show Gist options
  • Save micah1701/5364997 to your computer and use it in GitHub Desktop.
Save micah1701/5364997 to your computer and use it in GitHub Desktop.
Use the Google Maps API to geocode a given address.
<?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