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.
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.
Copy this API key for later use in the script on the Google Maps Geocoding API request.
The following example code shows steps to get address from given latitude and longitude using Google Geocoding API and PHP.
$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.https://maps.googleapis.com/maps/api/geocode/json
) using the file_get_contents() method in PHP.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;
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
Thank You for sharing us
How to use google api javascript ? without php
Hi every one which is related to web development personally i recommend you that I take swear of ALLAH that never i found it Such a nice website and error free Code……..
I love you author