地理坐标转街道名称

9

有没有任何方法(最好是使用REST API)获取与地理坐标对应的街道名称?我认为这个名字叫做地理编码,Google有提供这个API吗?我是PHP开发人员。

例如:

<?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) - Pascal MARTIN
4个回答

14

是的,只需在Google Maps 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有很多限制。例如,您不能在没有地图的情况下使用它,或者您每天查询的数量是有限制的。 - Ilya Saunkin

3

以下是我用于使用Google MAP API对街道地址进行反向地理编码查找的PHP函数。请注意,此示例从Google获取JSON输出,但我在PHP中进行了简单的解析。

/*
 * 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;
}

安德鲁 OpenGeoCode.Org 团队

顺便说一句> 谷歌对于商业用途使用他们的API有限制。基本上,你需要在谷歌地图上显示结果。


2
您还可以查看使用Yahoo!的PlaceFinder API,该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);

这段话的意思是:输出第一个包含属性如“street”、“postal”和“city”的结果(希望有这样的结果!)。
使用PlaceFinder API的另一种方式是通过Yahoo!的YQL API,它允许您针对“数据表”(通常是其他API)使用类似SQL的查询。
这样的查询可能如下所示:
SELECT * FROM geo.placefinder WHERE text="55.948496,-3.198909" AND gflags="R"

(在YQL控制台界面中尝试这个)

使用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);

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接