Get Address from Latitude and Longitude using PHP and Google Maps API

Hi! Today let's see how to get address from latitude and longitude using PHP and Google Maps Geocoding API. In general, the process of converting geometric coordinates such as latitude and longitude into address is called reverse geocoding. And the geocoding is just the opposite, converting address into latitude and longitude which we have seen in our previous tutorial. Google Maps provides a separate Geocoding API for the purpose and let's see how to use it with php.

To obtain physical addresses from the API, you have to send http request along with latitude and longitude values.

php get address from latitude longitude google maps api

Getting Address from Latitude and Longitude using Google Maps:

To access Google Maps Geocoding API, you need an http interface through which you can send and receive an http request/response from the api. Here is the sample request you should send to the web service.

http://maps.google.com/maps/api/geocode/json?latlng=40.6781784,-73.9441579

The 'latlng' parameter must be used to provide latitude, longitude values in the url. Also, to get the response as json, you must be specific on your api call.

Below is the reverse geocoding sample response that we get for the above api request,

{
   "results" : [
      {
         "address_components" : [
            {
               "long_name" : "58",
               "short_name" : "58",
               "types" : [ "street_number" ]
            },
            {
               "long_name" : "Brooklyn Avenue",
               "short_name" : "Brooklyn Ave",
               "types" : [ "route" ]
            },
            {
               "long_name" : "Crown Heights",
               "short_name" : "Crown Heights",
               "types" : [ "neighborhood", "political" ]
            },
            {
               "long_name" : "Brooklyn",
               "short_name" : "Brooklyn",
               "types" : [ "political", "sublocality", "sublocality_level_1" ]
            },
            {
               "long_name" : "Kings County",
               "short_name" : "Kings County",
               "types" : [ "administrative_area_level_2", "political" ]
            },
            {
               "long_name" : "New York",
               "short_name" : "NY",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "United States",
               "short_name" : "US",
               "types" : [ "country", "political" ]
            },
            {
               "long_name" : "11216",
               "short_name" : "11216",
               "types" : [ "postal_code" ]
            }
         ],
         "formatted_address" : "58 Brooklyn Ave, Brooklyn, NY 11216, USA",
         "geometry" : {
            "location" : {
               "lat" : 40.677978,
               "lng" : -73.94438700000001
            },
            "location_type" : "ROOFTOP",
            "viewport" : {
               "northeast" : {
                  "lat" : 40.67932698029149,
                  "lng" : -73.94303801970851
               },
               "southwest" : {
                  "lat" : 40.6766290197085,
                  "lng" : -73.94573598029152
               }
            }
         },
         "place_id" : "ChIJaVKlrIVbwokRhqlQjSdxUHc",
         "types" : [ "street_address" ]
      },
      
      ...
      ...
   ],
   "status" : "OK"
}

The response sends a status flag which you can test to determine the success or failure of the conversion process.

PHP Function to Convert Latitude and Longitude into Address:

Below is the php function to get the address details of the given geographic co-ordinates. You must pass longitude and latitude to the function which in turn makes http request to the api, receive json response, parse and return the formatted address.

<?php
function getAddress($latitude, $longitude)
{
        //google map api url
        $url = "http://maps.google.com/maps/api/geocode/json?latlng=$latitude,$longitude";

        // send http request
        $geocode = file_get_contents($url);
        $json = json_decode($geocode);
        $address = $json->results[0]->formatted_address;
        return $address;
}
?>

Usage:

You have to access the above getAddress() function like this,

<?php
// coordinates
$latitude = '40.6781784';
$longitude = '-73.9441579';
$result = getAddress($latitude, $longitude);
echo 'Address: ' . $result;

// produces output
// Address: 58 Brooklyn Ave, Brooklyn, NY 11216, USA
?>

The php script will output the location as a human-readable address.

API Key:

Without API Key, you're requests credits per day will be very limited. In order to use the API key in each of your requests, you must first activate the Google Maps API and obtain the authentication credentials for it.

Log in to your Google account, go to the API console, activate the 'Google Maps Geocoding API' and get an 'API_KEY'.

Then you can use this key in the url every time you send a request to the api in this way,

http://maps.google.com/maps/api/geocode/json?latlng=$latitude,$longitude&key=API_KEY;

Please note that the sensor param is deprecated in the latest versions of the API. Therefore, it is no longer necessary to attach it with the request URL.

Read Also:

That explains about getting location from latitude and longitude in php using Google Maps Geocoding API. You can use this address info to place a marker on Google Maps. I hope you like this tutorial. If you find this post useful, please share it in your social circle.

No comments:

Post a Comment