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

问题描述 投票:0回答:1

我如何从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"
}
json google-maps google-api
1个回答
0
投票

你应该使用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;
?>
© www.soinside.com 2019 - 2024. All rights reserved.