Using the Google API to determine the County from one of two choices (by latitude and longitude) is giving inconsistent results. One gives the County under the key of 2; the other with the key of 4. How can I fetch the needed information with this inconsistency?
I have used Json only a couple times so perhaps I've misunderstood how values are retrieved so here is my PHP function for parsing it:
function getCounty($LatLong) {
global $googlekey;
$url = "https://maps.googleapis.com/maps/api/geocode/json?latlng=$LatLong&key=$googlekey";
$json = @file_get_contents($url);
$data = json_decode($json);
$status = $data->status;
if ($status === "OK") :
//return $data->results[0]->address_components[2]->long_name;
return $data->results[0]->address_components[4]->long_name;
else :
return FALSE;
endif;
}
In other words, to get one County name it needs this:
return $data->results[0]->address_components[2]->long_name;
. . . while it needs this to get the other County name:
return $data->results[0]->address_components[4]->long_name;
This defeats the purpose of trying to find dynamically in which County it is being used!
Using Json to parse it, gives this (a partial snippet of the output) for one county where the key for the County is 2
[2] => stdClass Object
(
[long_name] => Santa Cruz County
[short_name] => Santa Cruz County
[types] => Array
(
[0] => administrative_area_level_2
[1] => political
)
)
[3] => stdClass Object
(
[long_name] => California
[short_name] => CA
[types] => Array
(
[0] => administrative_area_level_1
[1] => political
)
)
[4] => stdClass Object
(
[long_name] => United States
[short_name] => US
[types] => Array
(
[0] => country
[1] => political
)
)
. . . and this gives the key for the other County as 4:
[2] => stdClass Object
(
[long_name] => West Valley
[short_name] => West Valley
[types] => Array
(
[0] => neighborhood
[1] => political
)
)
[3] => stdClass Object
(
[long_name] => San Jose
[short_name] => San Jose
[types] => Array
(
[0] => locality
[1] => political
)
)
[4] => stdClass Object
(
[long_name] => Santa Clara County
[short_name] => Santa Clara County
[types] => Array
(
[0] => administrative_area_level_2
[1] => political
)
)
Having said that, is there a way to get the County directly from the IP address? I saw somewhere that Google frowns on it but then within the Google API documentation, I saw references to use the IP but no examples so it was vague on how it might be done.