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

Published by Sunny

A Mechanical engineer by education and web developer by passion and profession. He lives in Lucknow, India. Currently earning his livings by hitting keyboad. ;)

Join the Conversation

7 Comments

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

Leave a comment

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