2011-03-09 66 views
7

是以任何方式(与其余 API将很棒)获取与地理坐标相对应的街道名称?我认为这个名字是地理编码,谷歌有这个API吗? Im PHP开发人员。到街道名称的地理坐标

Ex。

<?php 
cor="38.115583,13.37579"; 
echo(geoname(cor)); // this prints: Foro Umberto I - 90133 Palermo 
?> 

所以该函数的输出是街道名称,邮政编码和城市。感谢任何帮助和脚本示例!

+1

你想要的不是地理编码,而是**反向**地理编码;-) *(见https://secure.wikimedia.org/wikipedia/en/wiki/Reverse_geocoding)* – 2011-03-09 22:42:15

回答

11

是,只使用谷歌地图API中的 “反向地址解析” 功能:http://code.google.com/apis/maps/documentation/geocoding/#ReverseGeocoding

这里的一些示例代码:

$lat="38.115583"; 
$long = "13.37579"; 

$url = "http://maps.googleapis.com/maps/api/geocode/json?latlng=$lat,$long&sensor=false"; 

$curl = curl_init(); 
curl_setopt($curl, CURLOPT_URL, $url); 
curl_setopt($curl, CURLOPT_HEADER, false); 
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true); 
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($curl, CURLOPT_ENCODING, ""); 
$curlData = curl_exec($curl); 
curl_close($curl); 

$address = json_decode($curlData); 
print_r($address); 
+2

这里有很多限制Google地图API。例如。你不能在没有地图的情况下使用它,或者你仅限于每天的查询数量。 – 2011-03-09 22:49:14

0

由于@Elijah提到,我在上次回答中提供的Maps API有很多限制。您实际上只能将其用于Google地图应用程序。如果这是一个问题,那么您也可以尝试AdWords API。它也有类似的服务,但Google会收费,所以限制较少。

2

你也可以看看使用雅虎的PlaceFinder API,它提供反向地理编码。对API的调用的小例子(要求它返回轻量级数据交换格式大谈特谈,JSON)可能看起来像:

$url  = 'http://where.yahooapis.com/geocode?location=55.948496,-3.198909&gflags=R&flags=J'; 
$response = json_decode(file_get_contents($url)); 
$location = $response->ResultSet->Results[0]; 
print_r($location); 

它输出的第一个结果(希望有一个!),其中包含属性如streetpostalcity


使用PlaceFinder API的另一种方式是通过雅虎YQL API,它使您可以使用类似于SQL的查询对‘数据表’(通常,其他API)。

这样的查询可能如下:

SELECT * FROM geo.placefinder WHERE text="55.948496,-3.198909" AND gflags="R" 

Try this in the YQL console interface

要拨打电话与该查询,从PHP到YQL,非常类似于早期的例子,应打印相同信息。

$query = 'SELECT * FROM geo.placefinder WHERE text="55.948496,-3.198909" AND gflags="R"'; 
$url  = 'http://query.yahooapis.com/v1/public/yql?q='.urlencode($query).'&format=json'; 
$response = json_decode(file_get_contents($url)); 
$location = $response->query->results->Result; 
print_r($location); 
3

这里是我的PHP函数,用于使用Google MAP API对街道地址进行反向地理编码查找。请注意,这个例子从JSON中获得Google的输出,但我在PHP中做了一个简单的解析。在OpenGeoCode.Org

/* 
* Use Google Geocoding API to do a reverse address lookup from GPS coordinates 
*/ 
function GetAddress($lat, $lng) 
{ 
    // Construct the Google Geocode API call 
    // 
    $URL = "http://maps.googleapis.com/maps/api/geocode/json?latlng=${lat},${lng}&sensor=false"; 

    // Extract the location lat and lng values 
    // 
    $data = file($URL); 
    foreach ($data as $line_num => $line) 
    { 
     if (false != strstr($line, "\"formatted_address\"")) 
     { 
      $addr = substr(trim($line), 22, -2); 
      break; 
     } 
    } 

    return $addr; 
} 

安德鲁 团队BTW>谷歌确实使用他们的API用于商业用途的限制。基本上,你需要在谷歌地图上显示结果。

相关问题