Codeigniter 4 Get Latitude and Longitude From Address
In this tutorial, we will show you how to get the latitude and longitude of an address in PHP CodeIgniter without displaying a Google Map. We will be using the Google Geocoding API to accomplish this.
To get started, you will need to obtain an API key from Google before you can make any calls to the Geocoding service. The API key is necessary for authentication purposes.
Once you have your API key, you can proceed to get the latitude and longitude of an address in PHP CodeIgniter using the Google Geocoding API. We will be using the “file_get_contents()” function in PHP to retrieve the JSON response from the API.
First, you will have to visit: https://cloud.google.com/maps-platform/?_ga=2.27293823.277869946.1577356888-568469380.1576660626#get-started and get the API key.
Steps of to get an API key From Google Console:
- Visit the Google Cloud Platform Console.
- Click the project drop-down and select or create the project for which you want to add an API key.
- Click the menu button and select APIs & Services > Credentials.
- On the Credentials page, click Create credentials > API key.
The API key created dialog displays your newly created API key. - Click Close.
The new API key is listed on the Credentials page under API keys.
(Remember to restrict the API key before using it in production.)
Get Latitude and Longitude From Address in Codeigniter 4 App
Follow the given steps to get Latitude and Longitude From The Address in Codeigniter 4 Using The Google Map Geocode API.
Step 1 – Use google API with key
https://maps.google.com/maps/api/geocode/json?address='.$address.'&key=your_api_key
Step 2 – Create functions to getting address
In this step, we need to create two functions with the name of getLocation() and getLongitudeLatitudeFromAddress() respectively, with the help of both the function we will try to find address from google map api
Explanation of created functions
The first function is called “getlocation()” and it takes an address as its input. Inside this function, we will call the second function called “get_longitude_latitude_from_address()” with the address parameter. This function will return the latitude and longitude values.
The “get_longitude_latitude_from_address()” function takes an address as its input and calls the Google Geocode API to retrieve the latitude and longitude values.
public function getlocation()
{
$address = "London united state";
$array = $this->get_longitude_latitude_from_adress($address);
$latitude = round($array['lat'], 6);
$longitude = round($array['long'], 6);
}
function get_longitude_latitude_from_adress($address){
$lat = 0;
$long = 0;
$address = str_replace(',,', ',', $address);
$address = str_replace(', ,', ',', $address);
$address = str_replace(" ", "+", $address);
try {
$json = file_get_contents('https://maps.google.com/maps/api/geocode/json?address='.$address.'&key=your_api_key');
$json1 = json_decode($json);
if($json1->{'status'} == 'ZERO_RESULTS') {
return [
'lat' => 0,
'lng' => 0
];
}
if(isset($json1->results)){
$lat = ($json1->{'results'}[0]->{'geometry'}->{'location'}->{'lat'});
$long = ($json1->{'results'}[0]->{'geometry'}->{'location'}->{'lng'});
}
} catch(exception $e) { }
return [
'lat' => $lat,
'lng' => $long
];
}
Note:- You can also place these functions in Codeigniter helper and call the function where you want to get latitude and longitude from address.
Conclusion
In this example, we have learned how to get the latitude and longitude values from an address in CodeIgniter 4 using Google APIs. We have created two functions, “getlocation()” and “get_longitude_latitude_from_address()”, that utilize the Google Geocode API to retrieve the latitude and longitude values from an address. This allows us to use this information in our application without having to display it on a map.
You May Also Like
- Codeigniter 4 Import Data to Excel/CSV File From MySQL Database
- Codeigniter 4 Dynamic Dependent Dropdown with Ajax
- Codeigniter 4 Autocomplete Textbox From Database using Typeahead JS
- Codeigniter 4 PDF Generator Tutorial Example
- CodeIgniter 4 Rest Api Example Tutorial
- Crop and Save Image using jQuery Coppie in Codeigniter 4
- Codeigniter 4 CRUD Operation Using Ajax Tutorial
- How To Update Session value in Codeigniter
- Codeigniter 4 CRUD with Bootstrap and MySQL Example
- How to Get Session Data in CodeIgniter
- CodeIgniter 4 Pagination Example Tutorial
- CodeIgniter 4 Multiple Image File Upload Example
- Codeigniter 4 Ajax Image Upload with Preview Example
- Codeigniter 4 Database & Email Config Example
- Codeigniter 4 jQuery Image Upload with Preview Example

