Codeigniter 4 Get Latitude and Longitude From Address - Yudhy Network

Share on:

Yudhy Network - Codeigniter 4 get latitude and longitude from address. Get latitude and longitude from address without showing google map using google api in codeigniter 4.

Get latitude and longitude from address without showing google map using google api in codeigniter 4. In this tutorial, you learn how to get latitude and longitude from an address in PHP Codeigniter.

If you need to get latitude and longitude from an address in PHP Codeigniter. Google has provided API that name google geocode API. In this tutorial, we will use this API and get latitude and longitude from the address in PHP Codeigniter.

First of all, You need get API key before you can make calls to the Google Maps Geocoding service.

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:

  1. Visit the Google Cloud Platform Console.
  2. Click the project drop-down and select or create the project for which you want to add an API key.
  3. Click the menu button  and select APIs & Services > Credentials.
  4. On the Credentials page, click Create credentials > API key.
    The API key created dialog displays your newly created API key.
  5. 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 below steps and get Latitude and Longitude From Address in Codeigniter using the google geocode API:

Step 1 – Use google API with key

https://maps.google.com/maps/api/geocode/json?address='.$address.'&key=your_api_key
Recommended:- CodeIgniter 4 jQuery Form Validation Example

Step 2 – Create functions to getting address

Next, we need to create two functions in the first is getlocation() and second one is get_longitude_latitude_from_adress().

Explanation of created functions

getlocation() :- When we pass the address in this function, inside this function we will call the get_longitude_latitude_from_adress() this function with address and it will return latitude and longitude from address.

get_longitude_latitude_from_adress() :- In this function, we will pass the address and this function call google geocode apis and get latitude and longitude from address.

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
 ];
}
Recommended:- CodeIgniter 4 Ajax Form Submit Validation Example

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

codeigniter get latitude and longitude from address example, you have learned how to get latitude and longitude from address without showing google map in codeigniter 4 using google apis.

Recommended Codeigniter Tutorials

Recommended:- CodeIgniter 4 Image Upload With Preview Example
Recommended:- CodeIgniter 4 Pagination Example Tutorial
Recommended:- Codeigniter 4 Ajax Image Upload with Preview Example
Recommended:- Codeigniter 4 PDF Generator Tutorial Example
Recommended:- CodeIgniter 4 First Crud Example Tutorial
Recommended:- CodeIgniter 4 Rest Api Example Tutorial
Recommended:- Codeigniter 4 Login And Registration Tutorial Example
Recommended:- Send Email in CodeIgniter 4 With SMTP
Recommended:- CodeIgniter 4 Ajax Crud with datatables and Bootstrap Modals
Recommended:- Crop and Save Image using jQuery Coppie in Codeigniter 4
Recommended:- Codeigniter 4 Dynamic Dependent Dropdown with Ajax
Recommended:- Codeigniter 4 Autocomplete Textbox From Database using Typeahead JS

Please ask in the comments!

Suport : Cotekno.Com

comments powered by Disqus