I'm using the google geocoding api. Everything is working fine. However when I search for 1 location I receive way to many information. I only need for example the administrative_area_level_1
.
Url that I use:
https://maps.googleapis.com/maps/api/geocode/json?latlng=12.1224221,-68.8824233&sensor=false&key=mykey:)
I receive this:
{
"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"
}
Right now I have to do this:
json_decode($response->getBody())->results[0]->address_components[4]->long_name;
And I get the administrative_area_level_1
.
Is there a better?
Reverse geocoding in Geocoding API also allows to specify a type of results that you want to get. In order to get only result of type administrative_area_level_1 add a result_type
parameter in your request
https://maps.googleapis.com/maps/api/geocode/json?latlng=12.1224221%2C-68.8824233&result_type=administrative_area_level_1&key=YOUR_API_KEY
The response will contain only administrative area level 1 item, so you can retrieve a name of administrative area as
json_decode($response->getBody())->results[0]->formatted_address
or
json_decode($response->getBody())->results[0]->address_components[0]->long_name
I hope this helps!