Skip to content

Find Latitude and Longitude of a Location using PHP

January 11, 2010

In one of my Recent Post, I had discussed about finding the latitude and longitude using the Google map API, Now I’m going to share about the same concept which is using PHP. Refer this Post for more details.

function getlatandlon($address)
{
	$address = urlencode($address);//India, Tamil nadu, chennai

	//If you want an extended data set, change the output to "xml"
 instead of csv
	$local = "ABQIAAAAkGMwVXYXPd1ca9opxkmJeRS0jHQK4ys7cQUygIGq
5srO1DwAzRTz5D0mZT53BCeHRWnBJ8DBtnsJ6w";
	$url = "http://maps.google.com/maps/geo?q=".$address."&
output=csv&key=".$local;
	//Set up a CURL request, telling it not to spit back headers,
 and to throw out a user agent.
	$ch = curl_init();

	curl_setopt($ch, CURLOPT_URL, $url);
	curl_setopt($ch, CURLOPT_HEADER,0); 
//Change this to a 1 to return headers
	curl_setopt($ch, CURLOPT_USERAGENT,
$_SERVER["HTTP_USER_AGENT"]);
	curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

	$data = curl_exec($ch);
	curl_close($ch);

	echo $data;
}

so you will get a output like this,

200,4,38.951667,-92.333889

And that’s it! Less than 40 lines of code to give you a powerful resource for mapping and usability. The first number is the return code, the second the relative accuracy (there 4 we see here is quite low as a result of the broad input. 8 is street-level address accuracy), third the latitude, and fourth the longitude.

Advertisements

From → PHP

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: