Easy PHP GeoCoding with Yahoo Maps API

I just recently began a project for a client that will require a proximity search feature. Basically the visitor inputs their location, either city/state combo or zip code along with a proximity value (within 10 miles, 25 miles, 50 miles etc) and the search returns a list of businesses (in this case distributors of the clients product line) within that area. I’ve seen these types of searches many times, but this is the first time I’ve had occasion to build one.

To even begin a project like this you would generally need to get your hands on some kind of database mapping city/states and zip codes to latitude and longitude values before you could even start wondering how to calculate a radial distance area from those locations. Traditionally when it comes to these databases, you can either buy the data which is usually a little pricey and may carry a monthly subscription fee as well. Or you can dig around various federal government websites looking for the data only to find it 5 or 6 years out of date and compiled in way that isn’t quite what you wanted. Frankly, either option kinda sucks. Thankfully we now have very sophisticated mapping services from Google, Yahoo and Mapquest that can be leveraged for the task of geocoding locations.

I’ve built a few google maps for clients, and their ajax enable maps are definitely slick. But for this project I’m just looking for an easy way to geocode a location into latitude/longitude coordinates. I plan to use PHP for the search functionality, and that’s where the Yahoo GeoCode API is a perfect fit.

See, while both Google and Yahoo provide an HTTP request method that takes the location string you provides and returns the geospatial data. Google’s API returns their results in XML (actually KML), JSON or CSV. But, Yahoo’s map API has the option of returning the data as a PHP serialized string! What that means is if you send a request to the Yahoo map API, you only have to run the results through PHP’s unserialize() function and you have your geodata in a neat and manageable PHP array. No need to fuss with parsing the XML, JSON or CSV results Google’s map API would return.

And here’s a little function to put that all together

function geocode($location, $app_id=NULL){
	$api_gateway = "http://local.yahooapis.com/MapsService/V1/geocode";
	$app_str = ( $app_id )? "appid=" . $app_id : "appid=YahooDem";
	$location_str = "location=" . urlencode($location);
	$output_str = "output=php";
	
	$request = $api_gateway . "?" . $app_str . "&" . $location_str . "&" . $output_str;
	
	$response = @file_get_contents($request);
	
	if( $response ){
		$result = unserialize(file_get_contents($request));		
	} else {
		$result = FALSE;
	}
	
	return $result;

}

Include that into your script and then call it like so:

$location = 90210;

$geodata = geocode($location);

if( is_array($geodata) ){
	print_r($geodata);
} else {
	echo "Location could not be found";
}

But the function isn’t limited to geocoding zip codes, it can also handle strings, like full addresses, or city/state combos. Just remember Yahoo’s map API limits your requests to 5,000 a day from the same IP.

1 Comment

  1. Rachit

    # May 16, 2010 - 8:31 pm

    This is amazing code. I have been trying to write a program similar to this, however, instead of using latitude/longitude coordinates I am trying to do a radius view of different cities. The program will allow the user to select a state/province and a radius in miles. Which then would display all the cities in that radius of the state. However, I am using mapquest and having a hard time serializing and retrieving data just for cities. I will try using this code and modify it for myself. Thanks

Leave a Reply