Skip to content

Instantly share code, notes, and snippets.

@ekka21
Created July 5, 2012 13:39
Show Gist options
  • Save ekka21/3053707 to your computer and use it in GitHub Desktop.
Save ekka21/3053707 to your computer and use it in GitHub Desktop.
Get lat and lng from google map api
<?php
function getLatandLong($addr,$city,$state)
{
global $lat;
global $lng;
$doc = new DOMDocument();
$doc->load("http://maps.google.com/maps/api/geocode/xml?address=".$addr.",+".$city.",+".$state."&sensor=false"); //input address
//traverse the nodes to get to latitude and longitude
$results = $doc->getElementsByTagName("result");
$results = $results->item(0);
$results = $results->getElementsByTagName("geometry");
$results = $results->item(0);
$results = $results->getElementsByTagName("location");
foreach($results as $result)
{
$lats = $result->getElementsByTagName("lat");
$lat = $lats->item(0)->nodeValue;
$lngs = $result->getElementsByTagName("lng");
$lng = $lngs->item(0)->nodeValue;
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment