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.

The Php Script

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

  # Call function
  echo getAddress("28.6292", "77.0805");
 ?>

This will output Janakpuri District Center, Janakpuri, New Delhi, Delhi 110058, India
You may also want a reverse process like Getting latitude and longitude coordinates from given address.
Get address from Latitude and Longitude Coordinates in php Get address from Latitude and Longitude Coordinates in php Reviewed by Altaf Hussain on 11:40 PM Rating: 5

19 comments:

  1. Thanks altab bhai..i appriciate ur work..keep it up..



    Salman Ahmad
    India
    blog:http://salmanahmd.blogspot.in/

    ReplyDelete
  2. Nice work. Saved my a lot time. Thanks.

    ReplyDelete
  3. I think there is a bug in this code. When I try to export the address to Microsoft Excel the characters are all looking funny. I think the address has problem with font. Please fix the code to display better font for copying into Excel

    ReplyDelete
    Replies
    1. There is nothing to do with the fonts here. the output is a simple text. May be there is a problem with your browser font settings. I have tested the script again and its working fine.

      Delete
  4. There is something wrong, when i enter the US co-ordinates it will show address of China can you help me ..

    ReplyDelete
    Replies
    1. you are entering wrong longitude. There is need of negative mark on Longitude for USA address

      Delete
  5. i can't get all address of 8000 entries

    ReplyDelete
  6. Nice script Thank Altaf..

    ReplyDelete
  7. C - Hexagon, Pandara Flats, India Gate, New Delhi, Delhi 110001, India

    ReplyDelete
  8. Really it works...Thanx alot Altaf Hussain.

    ReplyDelete
  9. very nice script .. thnx for uploading this

    ReplyDelete
  10. hi mate? its working very well apart from one thing. It brings building name or house number which i don't want to be. is there any way i can get only street name and postcode. thanks

    ReplyDelete

Altaf Web. Powered by Blogger.