In this post i would like explain about how did we get the complete address with using of longitude and latitude  using google map API and php code. With following simple steps we can retrieve the complete address.

Obtaining an address from latitude and longitude coordinates is a common requirement in location-based web applications. In this article, we will explore the process of achieving this functionality using PHP. By the end of this guide, you’ll have the knowledge and tools to effortlessly retrieve an address based on geographical coordinates, adding valuable location-based features to your web applications.

The following is may useful script to detect the address.

 

Step 1 : Signup for API key Google Map.

 

Step 2 : Follow the below php code:

1
2
3
4
5
6
7
8
9
<?php
if(isset($_GET['latitude']))
{
$latitude=$_GET['latitude'];
}
if(isset($_GET['longitude']))
{
$longitude=$_GET['longitude'];
}

Your API Key

1
$key= "Your API Key";

Call the below function to get the address:

1
2
3
4
5
$data=translateLatLngtoAddress($latitude,$longitude,$key);

echo "<pre>";
var_dump($data);
echo "</pre>";

Function for get the data :

1
2
3
4
5
6
7
8
9
10
11
12
13
function translateLatLngtoAddress($lat,$long,$key)
{
$lat=mb_convert_encoding($lat, 'UTF-8',mb_detect_encoding($lat, 'UTF-8, ISO-8859-1', true));
$long=mb_convert_encoding($long, 'UTF-8',mb_detect_encoding($long, 'UTF-8, ISO-8859-1', true));

$url="http://maps.google.com/maps/geo?q=$lat,$long&amp;output=csv&amp;sensor=false&amp;key=".$key;
$response = getGeolocation($url);

if (substr($response, 0, 3) === '200') {
return $geo = explode(',', $response);
}
return false;
}

Function is for get the location is getGeolocation:

1
2
3
4
5
6
7
8
9
10
11
12
function getGeolocation($url)
{

$init = curl_init();
curl_setopt($init, CURLOPT_URL, $url);
curl_setopt($init, CURLOPT_HEADER,0);
curl_setopt($init, CURLOPT_USERAGENT, $_SERVER["HTTP_USER_AGENT"]);
curl_setopt($init, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($init);
curl_close($init);
return $response;
}

In conclusion, the ability to retrieve an address from latitude and longitude using PHP is a valuable skill for web developers. This article has equipped you with the essential techniques and methods to implement this feature seamlessly in your web applications. Whether you’re building a location-based service, a mapping application, or need to enhance the user experience with geographic information, this knowledge empowers you to provide accurate and relevant address information based on coordinates. By mastering these techniques, you can unlock the full potential of location-based web development.


20 Comments

srinivas tamada · November 25, 2009 at 5:44 am

useful…

Hashim · December 2, 2009 at 10:35 am

Hi this code is not work

Warning: Wrong parameter count for mb_detect_encoding() in /opt/lampp/htdocs/map/map3.php on line 22

Warning: mb_convert_encoding() [function.mb-convert-encoding]: Unknown encoding “-8” in /opt/lampp/htdocs/map/map3.php on line 22

Warning: Wrong parameter count for mb_detect_encoding() in /opt/lampp/htdocs/map/map3.php on line 23

Warning: mb_convert_encoding() [function.mb-convert-encoding]: Unknown encoding “-8” in /opt/lampp/htdocs/map/map3.php on line 23

bool(false)

Anil Kumar Panigrahi · December 8, 2009 at 7:23 am

Hi Hashim,

Its working yaar, i place demo following url, we can find the details.

http://labs.anil2u.info/latlong.php?latitude=16.9662011&longitude=82.2243712

Thank you.

Dharmveer Motyar · December 16, 2009 at 12:51 pm

Useful but please write a post How to detect user’s country?
Thanx for your help anil.

Joomla Developers · January 18, 2010 at 6:18 am

The code was really helpful and easy to understand. Thanks Anil

Hi Dharmveer,

You can get the code for “How to detect user’s country” from:

http://www.phpclasses.org/browse/package/2363.html

vainfotech · September 10, 2010 at 5:48 am

It is very useful

Thanks

Sandeep Kumar · August 22, 2011 at 12:29 pm

Thanks… Its really working..

Brijesh and sandeep · September 11, 2011 at 6:23 am

Its really very usefull ….
thanks .

Nancy · March 15, 2012 at 9:00 pm

What does this do please
$url=”http://maps.google.com/maps/geo?q=$lat,$long&output=csv&sensor=false&key=”.$key;

Mayank · May 12, 2012 at 12:44 pm

Hi I am using the code but it is giving me bool(false) where i am using

echo “

";
var_dump($data);
echo "

“;

Please tell me what to do… whats wrong in my code

Anil Kumar Panigrahi · May 13, 2012 at 6:19 am

@Mayank, This code uses an older version of google map API. It will be working for v2. I will update the post using v3 API.

Sidieu · June 24, 2012 at 5:56 pm

Hi Anil,

is there any Java corresponding code please?

Srikanth · July 4, 2012 at 1:53 pm

It is really good..
Thanks

john · July 26, 2012 at 2:12 pm

plz help me with the line

$init = curl_init();

it gives error as

Fatal error: Call to undefined function curl_init();

Himanshu · August 26, 2012 at 10:58 am

Hi Mayank,

you are getting boolean(false) because from google api v3 google doesn’t return status which we are checking in function

if (substr($response, 0, 3) === ‘200’) {
return $geo = explode(‘,’, $response);

so replace above code with this one

if ($response!=”) {
return $geo = explode(‘,’, $response);

Precious Fab-cast Pvt. Ltd · February 13, 2013 at 1:00 pm

Great job Done ! Thanks for sharing with us !

ketan patil · February 21, 2013 at 1:47 pm

i got error

Fatal error: Call to undefined function curl_init() in C:\xampp\htdocs\PicTraveler_new\branches\1.0.0\Implementation\system\core\Loader.php(829) : eval()’d code on line 257

ashish · August 28, 2014 at 5:03 am

Demo @ http://labs.anil2u.info/latlong.php?latitude=16.9662011&longitude=82.2243712

Returns Null for all values

Top 10 most viewed posts in anil labs - Anil Labs · January 8, 2012 at 11:05 am

[…] 5)We get the address by latitude and longitude -using php code […]

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *