【问题标题】:How to convert GPS coordinates to a full address with php? [duplicate]如何使用 php 将 GPS 坐标转换为完整地址? [复制]
【发布时间】:2013-04-30 00:13:00
【问题描述】:

我的 GPS 坐标格式为 54.1456123 10.413456

如何使用 PHP 将它们转换为带有邮政编码、街道和城市的地址?

【问题讨论】:

标签: php google-maps-api-3 gps


【解决方案1】:

使用谷歌 API

$lat="54.1456123";
$long = "10.413456";

$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 并从他们那里获取 keyws 等。
  • 我在没有 Google API 密钥和 curl 的情况下尝试了它:$curlData=file_get_contents( $url); 有效,谢谢
  • 仅用于有限的查询
【解决方案2】:

不需要curl 也不需要 API 密钥:

function geo2address($lat,$long) {
    $url = "http://maps.googleapis.com/maps/api/geocode/json?latlng=$lat,$long&sensor=false";
    $curlData=file_get_contents(    $url);
    $address = json_decode($curlData);
    $a=$address->results[0];
    return explode(",",$a->formatted_address);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-04
    • 1970-01-01
    • 2021-03-17
    • 1970-01-01
    相关资源
    最近更新 更多