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

Latitude and longitude contain various information related to geo data. You can get geographic info including address from latitude & longitude coordinates. Google Geocoding API provides an easy way to get address from latitude and longitude. If you have latitude & longitude and want to find location or address, Google Maps API is very useful for converting latitude and longitude to address using PHP.

The address can be retrieved from latitude and longitude coordinates with Geocoding API using PHP. In this tutorial, we will show you how to get address from latitude and longitude in PHP using Google Maps Geocoding API.

Google Maps API Key

The Google Maps API Key is required to post HTTP request to Geocoding API and get response. Before getting started create an API key on Google Cloud console.

  • Go to the Google Cloud console.
  • Select the project from the Project drop-down menu at the top. If you don’t have an existing project, create a new one.
  • In the left navigation pane, select the APIs & Services » Credentials.
  • In the Credentials page, select CREATE CREDENTIALS » API key.
  • The API key will be created and a dialog will appear with the newly created API key.
  • Navigate to the Library page from the left menu panel and make sure the Geocoding API is enabled.

Copy this API key for later use in the script on the Google Maps Geocoding API request.

Get Address from Latitude and Longitude using PHP

The following example code shows steps to get address from given latitude and longitude using Google Geocoding API and PHP.

  • Set the required values in the defined variable.
    • $GOOGLE_API_KEY – Specify the Google API Key.
    • $latitude – Specify the latitude from which you want to retrieve the address.
    • $longitude – Specify the longitude from which you want to retrieve the address.
  • Format the latitude & longitude by adding a comma (,) between them.
  • Request Google Geocoding API (https://maps.googleapis.com/maps/api/geocode/json) using the file_get_contents() method in PHP.
  • Pass latlng and key parameters in the Geocoding API URL.
  • Decode JSON response returned by the Geocoding API using PHP json_decode() function.
  • Get address info from the formatted_address object of API response.
<?php 

// Google Maps API Key
$GOOGLE_API_KEY 'YOUR_API_KEY_HERE';

// Latitude & Longitude from which the address will be retrieved
$latitude '38.897952';
$longitude '-77.036562';

// Formatted latitude & longitude string
$formatted_latlng trim($latitude).','.trim($longitude);

// Get geo data from Google Maps API by lat lng
$geocodeFromLatLng file_get_contents("https://maps.googleapis.com/maps/api/geocode/json?latlng={$formatted_latlng}&key={$GOOGLE_API_KEY}");

// Decode JSON data returned by API
$apiResponse json_decode($geocodeFromLatLng);

// Retrieve address from API data
$formatted_address $apiResponse->results[0]->formatted_address;

?>

Additionally, you can get the latitude, longitude, and place ID using the following code.

$latitude  $apiResponse->results[0]->geometry->location->lat
$longitude $apiResponse->results[0]->geometry->location->lng;
$place_id $apiResponse->results[0]->place_id;

Display the address, latitude, longitude, and place ID on the web page.

// Render the address of the given latitude & longitude 
echo 'Address: '.$formatted_address;

echo 
'<br/>Place ID: '.$place_id;
echo 
'<br/>Latitude: '.$latitude;
echo 
'<br/>Longitude: '.$longitude;

Conclusion

Hope this simple script will be very useful for getting the address from latitude and longitude. If you want to use the Google Maps Geocode API to get the other information, see some other useful scripts from the below tutorial links.

Do you want to get implementation help, or enhance the functionality of this script? Click here to Submit Service Request

3 Comments

  1. Vivek Raj Said...
  2. Rezky Pradana Budihartono Said...
  3. Naqqash Rana Said...

Leave a reply

keyboard_double_arrow_up