如何从googleapi返回的json中获取地址

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何从googleapi返回的json中获取地址相关的知识,希望对你有一定的参考价值。

我如何从json获得google-api返回的地址

这是我的请求网址,我没有在我提供的示例网址中包含api

这是示例网址:

https://maps.googleapis.com/maps/api/geocode/json?latlng=55.567,56.786&key=xyz

如何获取google json返回的格式化地址?

这是我的json

{
   "results" : [
      {
         "address_components" : [
            {
               "long_name" : "452377",
               "short_name" : "452377",
               "types" : [ "postal_code" ]
            },
            {
               "long_name" : "Republic of Bashkortostan",
               "short_name" : "Republic of Bashkortostan",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "Russia",
               "short_name" : "RU",
               "types" : [ "country", "political" ]
            }
         ],
         "formatted_address" : "Republic of Bashkortostan, Russia, 452377",
         "geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : 55.655173,
                  "lng" : 57.0644709
               },
               "southwest" : {
                  "lat" : 55.36128,
                  "lng" : 56.6011809
               }
            },
            "location" : {
               "lat" : 55.50385139999999,
               "lng" : 56.900913
            },
            "location_type" : "APPROXIMATE",
            "viewport" : {
               "northeast" : {
                  "lat" : 55.655173,
                  "lng" : 57.0644709
               },
               "southwest" : {
                  "lat" : 55.36128,
                  "lng" : 56.6011809
               }
            }
         },
         "place_id" : "ChIJ904w9b1u3EMRc1XU4GlYSGs",
         "types" : [ "postal_code" ]
      },
      {
         "address_components" : [
            {
               "long_name" : "Karaidelsky District",
               "short_name" : "Karaidelsky District",
               "types" : [ "administrative_area_level_2", "political" ]
            },
            {
               "long_name" : "Republic of Bashkortostan",
               "short_name" : "Republic of Bashkortostan",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "Russia",
               "short_name" : "RU",
               "types" : [ "country", "political" ]
            }
         ],
         "formatted_address" : "Karaidelsky District, Republic of Bashkortostan, Russia",
         "geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : 56.076375,
                  "lng" : 57.7045048
               },
               "southwest" : {
                  "lat" : 55.463071,
                  "lng" : 56.230281
               }
            },
            "location" : {
               "lat" : 55.73996589999999,
               "lng" : 56.8548154
            },
            "location_type" : "APPROXIMATE",
            "viewport" : {
               "northeast" : {
                  "lat" : 56.076375,
                  "lng" : 57.7045048
               },
               "southwest" : {
                  "lat" : 55.463071,
                  "lng" : 56.230281
               }
            }
         },
         "place_id" : "ChIJCQ7oNrlx3EMRbyb2O6_k5OY",
         "types" : [ "administrative_area_level_2", "political" ]
      },
      {
         "address_components" : [
            {
               "long_name" : "Republic of Bashkortostan",
               "short_name" : "Republic of Bashkortostan",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "Russia",
               "short_name" : "RU",
               "types" : [ "country", "political" ]
            }
         ],
         "formatted_address" : "Republic of Bashkortostan, Russia",
         "geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : 56.53352,
                  "lng" : 60.00295010000001
               },
               "southwest" : {
                  "lat" : 51.57122589999999,
                  "lng" : 53.1579969
               }
            },
            "location" : {
               "lat" : 54.2312172,
               "lng" : 56.1645257
            },
            "location_type" : "APPROXIMATE",
            "viewport" : {
               "northeast" : {
                  "lat" : 56.53352,
                  "lng" : 60.00295010000001
               },
               "southwest" : {
                  "lat" : 51.57122589999999,
                  "lng" : 53.1579969
               }
            }
         },
         "place_id" : "ChIJ_UeUVQW02UMRNFQ0VFqKPHA",
         "types" : [ "administrative_area_level_1", "political" ]
      },
      {
         "address_components" : [
            {
               "long_name" : "Russia",
               "short_name" : "RU",
               "types" : [ "country", "political" ]
            }
         ],
         "formatted_address" : "Russia",
         "geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : 81.8581221,
                  "lng" : -169.045286
               },
               "southwest" : {
                  "lat" : 41.185353,
                  "lng" : 19.6403534
               }
            },
            "location" : {
               "lat" : 61.52401,
               "lng" : 105.318756
            },
            "location_type" : "APPROXIMATE",
            "viewport" : {
               "northeast" : {
                  "lat" : 70,
                  "lng" : 179
               },
               "southwest" : {
                  "lat" : 40,
                  "lng" : 27
               }
            }
         },
         "place_id" : "ChIJ-yRniZpWPEURE_YRZvj9CRQ",
         "types" : [ "country", "political" ]
      }
   ],
   "status" : "OK"
}
答案

你应该使用file_get_contents然后json_decode你的结果,你可以得到results[0]->formatted_address所需的答案,因为geocodezip建议你。

所以你应该使用

<?php
$GoogleApiResult = file_get_contents(https://maps.googleapis.com/maps/api/geocode/json?latlng=55.567,56.786&key=xyz);
$GoogleApiResultDecoded = json_decode($GoogleApiResult);
echo $GoogleApiResultDecoded->results[0]->formatted_address;
?>

以上是关于如何从googleapi返回的json中获取地址的主要内容,如果未能解决你的问题,请参考以下文章

如何在IOS中从经纬度查找地址?

如何从这种错误的 JSON 格式中获取所需的数据?

使用 npm 包 googleapis 在对用户进行身份验证后如何获取用户的电子邮件地址?

从外部 URL 获取 JSON 数据并将其以纯文本形式显示在 div 中

如何从 json 解码结果中获取格式化地址

如何快速从 HttpClient 返回的 JSON 中获取部分属性?