Get address from Latitude/Longitude in php

Conversion of geo-coordinates into address is known as Reverse Geocoding. In this script we are using Google map API because it’s free, fast and no API key required.

Rate Limit of Google reveres Geocoding is 2500 API calls per IP per day.

PHP function for Reveres Geocoding

<?
function getaddress($lat,$lng)
{
$url = 'http://maps.googleapis.com/maps/api/geocode/json?latlng='.trim($lat).','.trim($lng).'&sensor=false';
$json = @file_get_contents($url);
$data=json_decode($json);
$status = $data->status;
if($status=="OK")
return $data->results[0]->formatted_address;
else
return false;
}
?>

Pass latitude and longitude in getaddress() function. It will return address string on success otherwise return boolean false.

Example

<?
$lat= 26.754347; //latitude
$lng= 81.001640; //longitude
$address= getaddress($lat,$lng);
if($address)
{
echo $address;
}
else
{
echo "Not found";
}
?>

Liked It? Get Free updates in your Email

Delivered by feedburner

7 thoughts on “Get address from Latitude/Longitude in php

  1. Aditya
    #

    Very nice and simple code. thanks a lot

    Reply
  2. Manu
    #

    Hello Aditya,

    The api was working properly but now it is returning a wrong address for a given latitue and logitude

    Reply
  3. Mohammad Nazmul Hasan Pias
    #

    Nice tutorial . Thanks for sharing with us .

    But I faced a problem , sometimes when I run your code , address isn’t available for the same Latitude and longitude I used before and got address by same code.

    Any solution ?

    Thanks in Advance again .

    Reply
  4. Abhiram
    #

    How to get specific continent for lat and long

    Reply
  5. sayed mehdi
    #

    hi
    thank you very much
    how to set locate farsi?
    and get me farsi language address

    Reply
  6. kedar
    #

    very nice tutorial

    Reply
  7. mhmd
    #

    It not work for me online but work good on a localhost
    how can i solve this ?

    Reply

Leave a Reply

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