如何使用php将GPS坐标转换为完整地址?

4

我有一个GPS坐标,格式为54.1456123 10.413456

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


尝试使用Google Maps API 参考此帖 - limovala
这不是重复的问题:我在这里询问如何使用PHP实现这个功能。 - rubo77
2个回答

10

使用谷歌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进行注册并从中获取关键字等信息。 - Kevin_cl
1
我尝试了一下没有使用Google API密钥和curl的方法:$curlData=file_get_contents( $url);,这个方法可行,谢谢。 - rubo77
1
仅适用于有限的查询。 - Muhaimin

6

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

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