PHP 使用Google Maps API获取纬度和经度

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PHP 使用Google Maps 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;
		}
}		
?>

以上是关于PHP 使用Google Maps API获取纬度和经度的主要内容,如果未能解决你的问题,请参考以下文章