PCHomepage
Programmer
I'm working on what should be a simple function to get the name of one of two California counties. However, on the line highlighted below, address_components[3] for one county gives the proper value but for other it needs to use address_components[4] to get the proper value.
That is, for one of them address_components[4] pulls up the county name but for the other county it pulls up the state name; address_components[3] pulls up the county name but for the other county it gives the city name. Apparently the array is inconsistent so how I can I fetch what's needed?
Also, does the Google API have the ability to fetch the information from the IP address? Right now I'm using another service to convert the IP to latitude and longitude for use on the Google API but I would prefer to use the Google service for both if possible.
That is, for one of them address_components[4] pulls up the county name but for the other county it pulls up the state name; address_components[3] pulls up the county name but for the other county it gives the city name. Apparently the array is inconsistent so how I can I fetch what's needed?
PHP:
[COLOR=gray]function getCounty($LatLong) {
$url = "[URL unfurl="true"]http://maps.googleapis.com/maps/api/geocode/json?latlng=$LatLong&sensor=false";[/URL]
$json = @file_get_contents($url);
$data = json_decode($json);
$status = $data->status;
if ($status == "OK") :[/color]
[bold]return $data->results[0]->address_components[[COLOR=red]3[/color]]->long_name;[/bold]
[COLOR=gray]else :
return false;
endif;
}[/color]
Also, does the Google API have the ability to fetch the information from the IP address? Right now I'm using another service to convert the IP to latitude and longitude for use on the Google API but I would prefer to use the Google service for both if possible.