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

How to get the address from the json that is returned by googleapi

如何从 json 中获取地址 google-api returns

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

这里是例子url:

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 您的结果,您可以按照 geocodezip 的建议通过 results[0]->formatted_address 获得所需的答案。

所以你应该使用

<?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;
?>