Google api 地理编码只得到一些值
Google api geocoding only get some values
我正在使用 google geocoding api。一切正常。但是,当我搜索 1 个位置时,我会收到很多信息。例如,我只需要 administrative_area_level_1
。
我使用的Url:
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
。
还有更好的吗?
Geocoding API 中的 Reverse geocoding 还允许指定您想要获得的结果类型。为了仅获得 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
希望对您有所帮助!
我正在使用 google geocoding api。一切正常。但是,当我搜索 1 个位置时,我会收到很多信息。例如,我只需要 administrative_area_level_1
。
Url:
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
。
还有更好的吗?
Reverse geocoding 还允许指定您想要获得的结果类型。为了仅获得 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
希望对您有所帮助!