/, PHP/Intelligent Geocoding using PHP and Yahoo Web Services

Intelligent Geocoding using PHP and Yahoo Web Services

Yahoo provides excellent web services for geocoding which is precise and has a rich database of places around the world. Using this web service you can find latitude and longitude of any places. This web service comes with intelligence and is able to accept normal addresses. For example you can pass “White House, Washington”, “Beverly Hills” or “JFK Airport”. Beside that it has a very rich database compared to other available geocoding services like google. Today we will see how to use this API using PHP.

The access point of yahoo geocoding API is http://api.local.yahoo.com/MapsService/V1/geocode and it takes two parameter to find the geo code of any place. These are

1. appid : Which is a unique identifier string of your application. Keep it a readable one.
2. location: This is the address of the place you are searching for

Lets have a look at the following PHP function. It can take any place as a parameter and return an array of latitude and longitude using this web service.

function getGeoCode($address)
{
	$_url = 'http://api.local.yahoo.com/MapsService/V1/geocode';
	$_url .= sprintf('?appid=%s&location=%s',"DeveloperTutorials",rawurlencode($address));
	$_result = false;
	if($_result = file_get_contents($_url)) {
		preg_match('!<Latitude>(.*)</Latitude><Longitude>(.*)</Longitude>!U', $_result, $_match);
		$lng = $_match[2];
		$lat = $_match[1];
		return array("lat"=>$lat,"lng"=>$lng,"address"=>$address);
	}
	else
	return false;
}
	

When you call it like this


print_r(getGeoCode("White House"));

you will get the following output

Array
(
    [lat] => 38.897610
    [lng] => -77.036377
    [address] => White House
)

Or if you pass “Buckingham Palace” you will get the following output

Array
(
    [lat] => 51.500919
    [lng] => -0.141360
    [address] => Buckingham palace
)

2008-03-13T09:50:40+00:00 March 13th, 2008|General, PHP|0 Comments

About the Author:

Leave A Comment