Reputation: 926
I'm using google Geocode api to get co-ordinates and country from the custom locale i'm sending.
The response that i'm getting from this is
{
"results" : [
{
"address_components" : [
{
"long_name" : "13",
"short_name" : "13",
"types" : [ "street_number" ]
},
{
"long_name" : "Vernon Park",
"short_name" : "Vernon Park",
"types" : [ "route" ]
},
{
"long_name" : "Toa Payoh",
"short_name" : "Toa Payoh",
"types" : [ "neighborhood", "political" ]
},
{
"long_name" : "Singapore",
"short_name" : "Singapore",
"types" : [ "locality", "political" ]
},
{
"long_name" : "367835",
"short_name" : "367835",
"types" : [ "postal_code" ]
}
],
"formatted_address" : "13 Vernon Park, Singapore 367835",
"geometry" : {
"location" : {
"lat" : 1.340062,
"lng" : 103.880577
},
"location_type" : "ROOFTOP",
"viewport" : {
"northeast" : {
"lat" : 1.341410980291502,
"lng" : 103.8819259802915
},
"southwest" : {
"lat" : 1.338713019708498,
"lng" : 103.8792280197085
}
}
},
"place_id" : "ChIJbXRmsZAX2jERv_hlVeqBXtM",
"types" : [ "street_address" ]
}
],
"status" : "OK"
}
The php code that i wrote to get country from the response is,
foreach($response['results'][0]['address_components'] as $addressComponet){
if(in_array('country', $addressComponet['types'])) {
if($addressComponet['short_name'] != $addressComponet['long_name'])
$country = $addressComponet['long_name'];
else
$country = $addressComponet['long_name'];
}
}
$geometry = $response['results'][0]['geometry'];
$longitude = $geometry['location']['lng'];
$latitude = $geometry['location']['lat'];
$array = array(
'latitude' => $latitude,
'longitude' => $longitude,
'location_type' => $geometry['location_type'],
'country' => $country
);
where $response
contains the curl_exec response. This seems to have been working fine till yesterday. However, since today, I'm not getting the country string in the array. Was there any change in the api response that i'm unaware of?
Upvotes: 1
Views: 1794
Reputation: 32128
Typically Geocoding API returns address components that are relevant for address formatting of the country in question.
Typical addresses in Singapore are:
Block 123 Marina Avenue 2 #15-222 Singapore 123456
6 Ayer Rajah Crescent Singapore 139962
They don't include the country name, so Geocoding API doesn't return this address component as it's not relevant for address formatting.
UPDATE:
If you need to check the country of particular locale the feasible workaround is reverse geocoding of the coordinate of locale with result type set to country.
E.g.
Search '6 Ayer Rajah Crescent Singapore 139962'
You will get coordinate 1.294947,103.787603. Now execute reverse geocoding with result type equal to country.
The response will be
{
"results":[
{
"address_components":[
{
"long_name":"Singapore",
"short_name":"SG",
"types":[
"country","political"
]
}
],
"formatted_address":"Singapore",
"geometry":{
"bounds":{
"northeast":{
"lat":1.4784001,"lng":104.0945001
},
"southwest":{
"lat":1.1496,"lng":103.594
}
},
"location":{
"lat":1.352083,"lng":103.819836
},
"location_type":"APPROXIMATE",
"viewport":{
"northeast":{
"lat":1.4707592,"lng":104.0884808
},
"southwest":{
"lat":1.1587023,"lng":103.6055575
}
}
},
"place_id":"ChIJdZOLiiMR2jERxPWrUs9peIg",
"types":[
"country","political"
]
}
],
"status":"OK"
}
Upvotes: 0
Reputation: 926
I thought the response was changed, so i went ahead and filed a bug at the Google Maps API issues section.
https://code.google.com/p/gmaps-api-issues/issues/detail?id=11024
Turns out they added a new forward geocoder in the geocoding api. They also added a flag that redirects the requests to the old forward geocoder in that api.
Now the point is, since they upgraded the geocoding api, What should we do with the such locales present in the database? We were already using google maps autocomplete to prevent random useless false locales to going through the API.
Upvotes: 1