I am also trying to extract the "address_components" from the JSON data, but the array index changes if you do not submit an address line for example.
I am trying to get the "sublocality" and "administrative_area_level_1" from the "address_components" below.
The API sample is as follows:
{
"results" : [
{
"access_points" : [],
"address_components" : [
{
"long_name" : "10",
"short_name" : "10",
"types" : [ "street_number" ]
},
{
"long_name" : "Gillespie Street",
"short_name" : "Gillespie St",
"types" : [ "route" ]
},
{
"long_name" : "South Beach",
"short_name" : "South Beach",
"types" : [ "political", "sublocality", "sublocality_level_1" ]
},
{
"long_name" : "Durban",
"short_name" : "Durban",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Durban Metro",
"short_name" : "Durban Metro",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "KwaZulu-Natal",
"short_name" : "KZN",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "South Africa",
"short_name" : "ZA",
"types" : [ "country", "political" ]
},
{
"long_name" : "4001",
"short_name" : "4001",
"types" : [ "postal_code" ]
}
],
"formatted_address" : "10 Gillespie St, South Beach, Durban, 4001, South Africa",
"geometry" : {
"location" : {
"lat" : -29.859728,
"lng" : 31.039773
},
"location_type" : "ROOFTOP",
"viewport" : {
"northeast" : {
"lat" : -29.8583790197085,
"lng" : 31.0411219802915
},
"southwest" : {
"lat" : -29.8610769802915,
"lng" : 31.0384240197085
}
}
},
"place_id" : "ChIJGRCdW0mo9x4RcIwU_7S1xa8",
"plus_code" : {
"compound_code" : "42RQ+4W Durban, South Africa",
"global_code" : "5G2H42RQ+4W"
},
"types" : [ "street_address" ]
}
],
"status" : "OK"
}
If you submit a full address to the API, you can use the hard coded index like below: [7]
$json_a['results'][0]['address_components'][7]['long_name']
But it is not reliable to hard code the index [7] in this case.
So I tested some sample code from above post, it is working, but in some cases it does not return the "long_name" of the "sublocality" or "administrative_admin_level_1":
/* if no address, then the array index change */
// $json_a['results'][0]['address_components'][7]['long_name']
$area = array();
foreach ($json_a['results'][0]['address_components'] as $addrObj) {
foreach($addrObj['types'] as $type) {
if (substr_count($type, 'sublocality')>0) {
$area['short_name'] = $addrObj['short_name'];
$area['long_name'] = $addObj['long_name'];
// var_dump($addrObj);
}
if (substr_count($type, 'administrative_area_level_1')>0) {
$area['short_admin'] = $addrObj['short_name'];
$area['long_admin'] = $addObj['long_name'];
var_dump($addrObj);
}
}
}
In the var_dump() I do get the long_name value though: In this sample it must be "Kwazulu-Natal", but from the code above, it returns a NULL value.
array(3) { ["long_name"]=> string(13) "KwaZulu-Natal" ["short_name"]=> string(3) "KZN" ["types"]=> array(2) { [0]=> string(27) "administrative_area_level_1" [1]=> string(9) "political" } }
Below is the full script I am trying to get to work. The lon, lng and format name is working 100%, it is just this array index problem.
/* get google result in JSON*/
$googleString = file_get_contents($googleUrl); // get json content
$json_a = json_decode($googleString, true); //json decoder
// response status will be 'OK', if able to geocode given address
if($json_a['status']=='OK'){
// get the important data
$lati = isset($json_a['results'][0]['geometry']['location']['lat']) ? $json_a['results'][0]['geometry']['location']['lat'] : "";
$longi = isset($json_a['results'][0]['geometry']['location']['lng']) ? $json_a['results'][0]['geometry']['location']['lng'] : "";
$formatted_address = isset($json_a['results'][0]['formatted_address']) ? $json_a['results'][0]['formatted_address'] : "";
/* if no address, then the array index change */
// $json_a['results'][0]['address_components'][7]['long_name']
$area = array();
foreach ($json_a['results'][0]['address_components'] as $addrObj) {
foreach($addrObj['types'] as $type) {
if (substr_count($type, 'sublocality')>0) {
$area['short_name'] = $addrObj['short_name'];
$area['long_name'] = $addObj['long_name'];
// var_dump($addrObj);
}
if (substr_count($type, 'administrative_area_level_1')>0) {
$area['short_admin'] = $addrObj['short_name'];
$area['long_admin'] = $addObj['long_name'];
var_dump($addrObj);
}
}
}
Is there a better way to get the "sublocality" and "administrative_area_level_1" from the array $area array above. Any pointers will be much appreciated.