Codeigniter 4 – Get Address from Latitude and Longitude Ex

How to get latitude and longitude from an address in PHP Codeigniter 4 example; in this tutorial, you will find out the simple way to get latitude and longitude from an address in Codeigniter using the Google API. However, without using Google Map.

For get address from latitude and longitude in codeigniter 4 app, you have create google api key. And as well as create two function getlocation() and getAddress() to get address using latitude and longitude. Here you can find step by step guide for how get address from latitude and longitude in codeigniter 4.

Steps To Get Latitude and Longitude From Address in Codeigniter 4

  • Step 1 – Create Google API
  • Step 2 – Use google API with key
  • Step 3 – Create functions to getting address

Step 1 – Create Google API

First of all, You need get API key before you can make calls to the Google Geocoding service. So, visit: https://cloud.google.com/maps-platform/?_ga=2.27293823.277869946.1577356888-568469380.1576660626#get-started and follow the below given steps:

  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.)

Step 2 – Use Google API with key

Now, you need to add google api key with the following script, as shown below:

https://maps.google.com/maps/api/geocode/json?address='.$address.'&key=your_api_key

Step 3 – Create functions to getting address

Here, you need to create the getlocation and getAddress() function, as shown below:

public function getlocation()
{
    $address = "Chennai India";
    $array  = $this->getAddress($address);
    $latitude  = round($array['lat'], 6);
    $longitude = round($array['long'], 6);           
}
 
function getAddress($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 [
            'latitude' => 0,
            'longitude' => 0
            ];
        }
 
 if(isset($json1->results)){
    
    $lat = ($json1->{'results'}[0]->{'geometry'}->{'location'}->{'latitude'});
    $long = ($json1->{'results'}[0]->{'geometry'}->{'location'}->{'longitude'});
  }
  } catch(exception $e) { }
    return [
    'latitude' => $latitude,
    'longitude' => $longitude
    ];
}

The getlocation() method helps to invoke the getAddress() function; consequently, it returns the address based on latitude and longitude. Similarly, the getAddress() method gets address based on lat and long using the Google Maps API.

Recommended Codeigniter 4 Post

  1. How to Install / Download Codeigniter 4 By Manual, Composer, Git
  2. How to Remove Public and Index.php From URL in Codeigniter 4
  3. Codeigniter 4 Form Validation Example Tutorial
  4. How to add jQuery Validation on Form in Codeigniter 4 Example
  5. Codeigniter 4 Ajax Form Submit Validation Example
  6. Codeigniter 4 File Upload Validation Example
  7. Image Upload with Validation in Codeigniter 4
  8. Codeigniter 4 Image Upload Preview Using jQuery Example
  9. Codeigniter 4 Ajax Image Upload Preview Example
  10. How to Upload Multiple Images in Codeigniter 4
  11. Codeigniter 4 Multiple Image Upload with Preview
  12. Codeigniter 4 Pagination Example; Create Pagination in Codeigniter
  13. Simple Codeigniter 4 CRUD with Bootstrap and MySQL Example
  14. Codeigniter 4 CRUD with Datatables Example
  15. Codeigniter 4 Image Crop and Save using Croppie Example
  16. Codeigniter 4 Dependent Dropdown using jQuery Ajax Example
  17. CodeIgniter 4 Rest Api CRUD Example
  18. Codeigniter 4 Login Registration and Logout Example

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *