Wednesday 19 August 2015

Get address from Latitude and Longitude Coordinates in php

Get address from Latitude and Longitude Coordinates in php Get Address from given Latitude and Longitude Coordinates using google map api called Reverse Geocoding. It is the process of converting geographic coordinates (Latitude and Longitude) into addresses. Here is a little php function to get address from latitude and longitude.

previously explain about getting latitude and longitude values from Google Maps API, based on the town, city or country location.

PHP function for Reveres Geocoding
<?php
    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.
<?php
 $lat= 26.754347; //latitude
 $lng= 81.001640; //longitude
$address= getaddress($lat,$lng);
 if($address){
   echo $address;
 }else{
   echo "Not found";
  }
?>

No comments:

Post a Comment