Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Google API Result Inconsistency

Status
Not open for further replies.

PCHomepage

Programmer
Feb 24, 2009
609
US
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?

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.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top