Return to Snippet

Revision: 32396
at September 23, 2010 14:26 by tcam27


Initial Code
<?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;
		}
}		
?>

Initial URL

                                

Initial Description
When creating a custom Google Map, the latitude and longitude for the place of interest are needed. Enter in the Address, City, and State, and the lat and lng pair will be returned.

Initial Title
Use Google Maps API to Get Latitude and Longitude

Initial Tags
google

Initial Language
PHP