We get the address by latitude and longitude -using php code

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.

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:

<?php
if(isset($_GET['latitude']))
{
$latitude=$_GET['latitude'];
}
if(isset($_GET['longitude']))
{
$longitude=$_GET['longitude'];
}

Your API Key

$key= "Your API Key";

Call the below function to get the address:

$data=translateLatLngtoAddress($latitude,$longitude,$key);

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

Function for get the data :

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:

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;
}

Hope thatĀ  it may be useful.

Thank you,

 

Anil Kumar Panigrahi

With more than 10 years experience in PHP and Founder of Anil Labs, a blog for PHP and related posts. contributed posts regarding CodeIgniter, CakePHP and Learn PHP online

Contact : [email protected]

You may also like...

20 Responses

  1. Hashim says:

    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)

  2. 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.

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

  4. 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

  5. vainfotech says:

    It is very useful

    Thanks

  6. Sandeep Kumar says:

    Thanks… Its really working..

  7. Brijesh and sandeep says:

    Its really very usefull ….
    thanks .

  8. Nancy says:

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

  9. Mayank says:

    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

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

  11. Sidieu says:

    Hi Anil,

    is there any Java corresponding code please?

  12. Srikanth says:

    It is really good..
    Thanks

  13. john says:

    plz help me with the line

    $init = curl_init();

    it gives error as

    Fatal error: Call to undefined function curl_init();

  14. Himanshu says:

    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);

  15. Great job Done ! Thanks for sharing with us !

  16. ketan patil says:

    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

  1. January 8, 2012

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

Leave a Reply

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