我可以在我的网站PHP上从用户那里获得完整的地址吗?



现在我使用以下代码来获取地址:

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

有结果(简化。你们可以在这里看到完整的结果:http://maps.googleapis.com/maps/api/geocode/json?latlng=10.8142,106.6438&sensor=false(:

 {
       "results" : [
          {
             "address_components" : [
                {
                   "long_name" : "C 2",
                   "short_name" : "C 2",
                   "types" : [ "route" ]
                },
                {
                   "long_name" : "Tân Bình",
                   "short_name" : "Tân Bình",
                   "types" : [ "administrative_area_level_2", "political" ]
                },
                {
                   "long_name" : "Hồ Chí Minh",
                   "short_name" : "Hồ Chí Minh",
                   "types" : [ "administrative_area_level_1", "political" ]
                },
                {
                   "long_name" : "Việt Nam",
                   "short_name" : "VN",
                   "types" : [ "country", "political" ]
                }
             ],
             "formatted_address" : "C 2, Tân Bình, Hồ Chí Minh, Việt Nam"
...
       "status" : "OK"
    }

它现在缺少街道名称。我想获得更多信息,例如街道名称,门牌号...如何获取用户的完整地址?

谢谢大家!

您仍然获得完整的地址,只是谷歌没有提供您搜索的所有地点的街道地址。一个例子

http://maps.googleapis.com/maps/api/geocode/json?latlng=44.4647452,7.3553838&sensor=false

http://maps.googleapis.com/maps/api/geocode/json?latlng=28.5355,77.3910&sensor=false

请参阅这两个示例。

最新更新