Get address from latlng php

 google_getCountry($jsondata),
    'province' => google_getProvince($jsondata),
    'city' => google_getCity($jsondata),
    'street' => google_getStreet($jsondata),
    'postal_code' => google_getPostalCode($jsondata),
    'country_code' => google_getCountryCode($jsondata),
    'formatted_address' => google_getAddress($jsondata),
);

return $address;
}

/* 
* Check if the json data from Google Geo is valid 
*/

function check_status($jsondata) {
    if ($jsondata["status"] == "OK") return true;
    return false;
}

/*
* Given Google Geocode json, return the value in the specified element of the array
*/

function google_getCountry($jsondata) {
    return Find_Long_Name_Given_Type("country", $jsondata["results"][0]["address_components"]);
}
function google_getProvince($jsondata) {
    return Find_Long_Name_Given_Type("administrative_area_level_1", $jsondata["results"][0]["address_components"], true);
}
function google_getCity($jsondata) {
    return Find_Long_Name_Given_Type("locality", $jsondata["results"][0]["address_components"]);
}
function google_getStreet($jsondata) {
    return Find_Long_Name_Given_Type("street_number", $jsondata["results"][0]["address_components"]) . ' ' . Find_Long_Name_Given_Type("route", $jsondata["results"][0]["address_components"]);
}
function google_getPostalCode($jsondata) {
    return Find_Long_Name_Given_Type("postal_code", $jsondata["results"][0]["address_components"]);
}
function google_getCountryCode($jsondata) {
    return Find_Long_Name_Given_Type("country", $jsondata["results"][0]["address_components"], true);
}
function google_getAddress($jsondata) {
    return $jsondata["results"][0]["formatted_address"];
}

/*
* Searching in Google Geo json, return the long name given the type. 
* (If short_name is true, return short name)
*/

function Find_Long_Name_Given_Type($type, $array, $short_name = false) {
    foreach( $array as $value) {
        if (in_array($type, $value["types"])) {
            if ($short_name)    
                return $value["short_name"];
            return $value["long_name"];
        }
    }
}

/*
*  Print an array
*/

function d($a) {
    echo "
";
    print_r($a);
    echo "
"; }

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.

Get address from latlng php

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.

results[0]->formatted_address;
        return $address;
}
?>

Usage:

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

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:

  • Get YouTube Video Title, Description and Thumbnail in PHP
  • Store and Retrieve Images from MySQL Database using PHP

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.

How can I get full address from latitude and longitude in PHP?

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

How do you find the geocoding of an address?

The Geocoding toolbar provides a quick way to search for an address and display the corresponding location on a map. If the Geocoding toolbar has not been added to ArcMap, click Customize > Toolbars > Geocoding to add it. Click the first drop-down arrow and choose an address locator.

How can I get latitude and longitude from Google Map in PHP?

Get Latitude & Longitude from Google Geocoding API in PHP.
// Google Geocoding API. Key..
$apiKey = 'API KEY. GOES HERE';.
$address = urlencode( '1600 Amphitheatre. ... .
$url = "https. ://maps.googleapis. ... .
$resp = json_decode( file_get_contents( ... .
// Latitude and. Longitude (PHP 7. ... .
$lat = $resp['results'][0] ... .
$long = $resp['results'][0].

What is LatLng in Google Map?

A LatLng is a point in geographical coordinates: latitude and longitude. Latitude ranges between -90 and 90 degrees, inclusive.