谷歌 api 地理编码只能获取一些值



我正在使用谷歌地理编码API。一切正常。但是,当我搜索 1 个位置时,我会收到许多信息。例如,我只需要administrative_area_level_1.

我使用的网址:

https://maps.googleapis.com/maps/api/geocode/json?latlng=12.1224221,-68.8824233&sensor=false&key=mykey:)

我收到这个:

{
"results": [
{
"address_components": [
{
"long_name": "Kaya Toni Kunchi",
"short_name": "Kaya Toni Kunchi",
"types": [
"route"
]
},
{
"long_name": "Willemstad",
"short_name": "Willemstad",
"types": [
"locality",
"political"
]
},
{
"long_name": "Curaçao",
"short_name": "Curaçao",
"types": [
"administrative_area_level_1",
"political"
]
},
{
"long_name": "Curaçao",
"short_name": "CW",
"types": [
"country",
"political"
]
}
],
"formatted_address": "Kaya Toni Kunchi, Willemstad, Curaçao",
"geometry": {
"bounds": {
"northeast": {
"lat": 12.1237013,
"lng": -68.88351919999999
},
"southwest": {
"lat": 12.1234992,
"lng": -68.8842526
}
},
"location": {
"lat": 12.123662,
"lng": -68.88392089999999
},
"location_type": "GEOMETRIC_CENTER",
"viewport": {
"northeast": {
"lat": 12.1249492302915,
"lng": -68.88253691970849
},
"southwest": {
"lat": 12.1222512697085,
"lng": -68.88523488029149
}
}
},
"place_id": "ChIJNcHwFUB9g44RuzTE4FqQ3UE",
"types": [
"route"
]
},
{
"address_components": [
{
"long_name": "Santa Rosa",
"short_name": "Santa Rosa",
"types": [
"political",
"sublocality",
"sublocality_level_1"
]
},
{
"long_name": "Willemstad",
"short_name": "Willemstad",
"types": [
"locality",
"political"
]
},
{
"long_name": "Santa Maria",
"short_name": "Santa Maria",
"types": [
"administrative_area_level_2",
"political"
]
},
{
"long_name": "Curaçao",
"short_name": "Curaçao",
"types": [
"administrative_area_level_1",
"political"
]
},
{
"long_name": "Curaçao",
"short_name": "CW",
"types": [
"country",
"political"
]
}
],
"formatted_address": "Santa Rosa, Willemstad, Curaçao",
"geometry": {
"bounds": {
"northeast": {
"lat": 12.1251383,
"lng": -68.8636779
},
"southwest": {
"lat": 12.1082708,
"lng": -68.8835479
}
},
"location": {
"lat": 12.1136041,
"lng": -68.87348109999999
},
"location_type": "APPROXIMATE",
"viewport": {
"northeast": {
"lat": 12.1251383,
"lng": -68.8636779
},
"southwest": {
"lat": 12.1082708,
"lng": -68.8835479
}
}
},
"place_id": "ChIJLbPjNBJ9g44R9mOepLEBeF4",
"types": [
"political",
"sublocality",
"sublocality_level_1"
]
},
{
"address_components": [
{
"long_name": "Willemstad",
"short_name": "Willemstad",
"types": [
"locality",
"political"
]
},
{
"long_name": "Santa Maria",
"short_name": "Santa Maria",
"types": [
"administrative_area_level_2",
"political"
]
},
{
"long_name": "Curaçao",
"short_name": "Curaçao",
"types": [
"administrative_area_level_1",
"political"
]
},
{
"long_name": "Curaçao",
"short_name": "CW",
"types": [
"country",
"political"
]
}
],
"formatted_address": "Willemstad, Curaçao",
"geometry": {
"bounds": {
"northeast": {
"lat": 12.1857187,
"lng": -68.8053561
},
"southwest": {
"lat": 12.0711749,
"lng": -68.9916729
}
},
"location": {
"lat": 12.1224221,
"lng": -68.8824233
},
"location_type": "APPROXIMATE",
"viewport": {
"northeast": {
"lat": 12.1857187,
"lng": -68.8053561
},
"southwest": {
"lat": 12.0711749,
"lng": -68.9916729
}
}
},
"place_id": "ChIJVy-mDF-ChI4R701VMbSSSR8",
"types": [
"locality",
"political"
]
},
{
"address_components": [
{
"long_name": "Curaçao",
"short_name": "Curaçao",
"types": [
"administrative_area_level_1",
"political"
]
},
{
"long_name": "Curaçao",
"short_name": "CW",
"types": [
"country",
"political"
]
}
],
"formatted_address": "Curaçao",
"geometry": {
"bounds": {
"northeast": {
"lat": 12.4151186,
"lng": -68.6109924
},
"southwest": {
"lat": 11.9627538,
"lng": -69.18834029999999
}
},
"location": {
"lat": 12.0989103,
"lng": -68.8585774
},
"location_type": "APPROXIMATE",
"viewport": {
"northeast": {
"lat": 12.4151186,
"lng": -68.6109924
},
"southwest": {
"lat": 11.9627538,
"lng": -69.18834029999999
}
}
},
"place_id": "ChIJtWah-X1ig44RMbWx9gCSTJg",
"types": [
"administrative_area_level_1",
"political"
]
},
{
"address_components": [
{
"long_name": "Curaçao",
"short_name": "CW",
"types": [
"country",
"political"
]
}
],
"formatted_address": "Curaçao",
"geometry": {
"bounds": {
"northeast": {
"lat": 12.4941999,
"lng": -68.5670001
},
"southwest": {
"lat": 11.9224,
"lng": -69.29899999999999
}
},
"location": {
"lat": 12.16957,
"lng": -68.99002
},
"location_type": "APPROXIMATE",
"viewport": {
"northeast": {
"lat": 12.4941999,
"lng": -68.5670001
},
"southwest": {
"lat": 11.9224,
"lng": -69.29899999999999
}
}
},
"place_id": "ChIJx_3HYr6dhI4RI0fG2yiSf_w",
"types": [
"country",
"political"
]
}
],
"status": "OK"
}

现在我必须这样做:

json_decode($response->getBody())->results[0]->address_components[4]->long_name;

我明白administrative_area_level_1.

有更好的吗?

地理编码 API 中的反向地理编码还允许指定要获取的结果类型。为了仅获取类型administrative_area_level_1的结果,请在请求中添加result_type参数

https://maps.googleapis.com/maps/api/geocode/json?latlng=12.1224221%2C-68.8824233&result_type=administrative_area_level_1&key=YOUR_API_KEY

响应将仅包含行政区域级别 1 项,因此您可以检索行政区域的名称作为

json_decode($response->getBody())->results[0]->formatted_address

json_decode($response->getBody())->results[0]->address_components[0]->long_name

我希望这有帮助!

相关内容

  • 没有找到相关文章

最新更新