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

时间:2017-06-12 10:28:46

标签: php google-maps

现在我使用此代码获取地址:

$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"
    }

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

谢谢你们!

1 个答案:

答案 0 :(得分:0)

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

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

看两个例子。

相关问题