如何从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中获取地址的主要内容,如果未能解决你的问题,请参考以下文章
使用 npm 包 googleapis 在对用户进行身份验证后如何获取用户的电子邮件地址?