How to get the location using latitude and longitude, get location name using latitude and latitude in PHP, google cloud console, google maps API Key

How to get the location using latitude and longitude

Suppose you may be working with google maps and you use google map functions that collects the user’s longitude and latitude and you are interested to get the actual location name using the coordinates you have received. With PHP and free tools, you can achieve this using a few steps that we are going to highlight.

One thing we all know, the easiest way to know the position of a person is through the use of a pin (mostly for WhatsApp users understand this better since we do all share location and we refer it to as a pin). A Pin, to understand better is made up of 2 major components, that is, Longitude and Latitude though there are others that are included. This pin is what google translates in the map by mapping it in the Google Maps software and plots where the two coordinates meet. Latitude coordinates are measured horizontally while longitude coordinates are measured vertically.

To go straight to getting the location name using latitude and longitude, we need first to have an API key. This key is derived from the google cloud console where you create an account and add a project. In the beginning, google allows you to use their services free for a certain period of time, and when the trial period expires which is not as close as you think they now start billing you.

To get your key in the google cloud console, navigate to the APIs & Services section and select credentials, next create your own credentials and your API key will be generated. The google cloud console looks like the one shown below

How to get the location using latitude and longitude in PHP

Having received the credentials and you have your coordinates, for this article, I will use

  • Latitude            -1.292066
  • Longitude         36.821945

Next, there is this link that google provides you with where you key in your latitude and latitude values, and your API key,longitude_value&sensor=true&key=your_api_key

Next, we will create a php file where we will use CURL to submit data to google server and then then it will be used to display response (if you a new to CURL, search for articles related to CURL in this page)

The php file that we will use is as shown below

$apikey="AIzaSyCEIww7-RMfKNxliXe0AN6IjPiQDXXXXXX"; //replace with your own API key
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $googleurl);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
$curl_response = curl_exec($curl);
echo $curl_response;

Next, run your file either in browser or postman or in your app, it will display a result will a lot of information as shown in the screenshot below

How to get the location using latitude and longitude in PHP

To interpret your result you need to narrow down and get the values

In the response you can use json_decode, for example, to get the values in the results array you can use

echo json_encode(json_decode($curl_response)->results);

This will refine the results further where you will be able to view more refined results like the city of the coordinates and so on.

Use the extract function until you get the component you want, that is, city or any other component from the array



Our discussion above has been showing how to use longitude and latitude from a given set of data and use those coordinates to get a place or location name and we have described what a pin as we normally work is made up. Follow this page so that you can learn on the next items related to this topic