【问题标题】:Geographical coordinates to street names街道名称的地理坐标
【发布时间】:2011-03-09 22:38:53
【问题描述】:

有什么方法(使用 rest API 会很棒)来获取与地理坐标对应的街道名称吗?我认为名称是地理编码,谷歌有这个 API 吗?我是 PHP 开发人员。

例如。

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

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

【问题讨论】:

标签: php api geolocation


【解决方案1】:

是的,只需使用 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);

【讨论】:

  • Google 地图 API 有很多限制。例如。没有地图就无法使用它,或者您每天的查询量有限。
【解决方案2】:

这是我使用 Google MAP API 对街道地址进行反向地理编码查找的 PHP 函数。请注意,此示例以 JSON 格式从 Google 获取输出,但我在 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 团队

btw> Google 确实对将其 API 用于商业目的有所限制。基本上,您需要在谷歌地图上显示结果。

【讨论】:

    【解决方案3】:

    您还可以看看使用 Yahoo! 的 PlaceFinder API,它提供反向地理编码。调用 API 的最小示例(要求它返回轻量级数据交换格式 du jour,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 的另一种方法是通过 Yahoo! 的 YQL API,它允许您对“数据表”(通常是其他 API)使用类似 SQL 的查询。

    这样的查询可能如下所示:

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

    【讨论】:

      【解决方案4】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-01-30
        • 2014-01-26
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多