How to get latitude and longitude from address in Codeigniter 4

You can use the Google Maps Geocoding API to get the latitude and longitude from an address in Codeigniter 4.

First, you need to get an API key from the Google Developer Console.

Once you have the API key, you can use the following code to get the latitude and longitude from an address:

// Get the address from the form $address = $this->input->post(‘address’); // Initialize the cURL session $ch = curl_init(); // Set the URL curl_setopt($ch, CURLOPT_URL, ‘https://maps.googleapis.com/maps/api/geocode/json?address=’.urlencode($address).’&key=YOUR_API_KEY’); // Set the request method to GET curl_setopt($ch, CURLOPT_CUSTOMREQUEST, ‘GET’); // Set the response as a string curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // Execute the request $response = curl_exec($ch); // Close the cURL session curl_close($ch); // Decode the response $responseData = json_decode($response); // Get the latitude and longitude from the response $latitude = $responseData->results[0]->geometry->location->lat; $longitude = $responseData->results[0]->geometry->location->lng;

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

So, In this tutorial, you learn how to get latitude and longitude from an address without showing google maps in PHP Codeigniter.

Before you start, You need to get an 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 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.)

How to get latitude and longitude from address in Codeigniter 4

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
  • Step 2 – Create functions to get address

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 get address

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

Explanation of created functions

  • geolocation():- This function takes an address as input and internally utilizes the get_longitude_latitude_from_address() function. By invoking get_longitude_latitude_from_address() with the provided address, it retrieves the corresponding latitude and longitude values, which are then returned by the geolocation() function.
  • get_longitude_latitude_from_address():- This function takes an address as input and utilizes the Google Geocoding API to retrieve the latitude and longitude coordinates associated with the given 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
 ];
}

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 Posts

Jaspreet Singh Ghuman

Jaspreet Singh Ghuman

Jassweb.com/

Passionate Professional Blogger, Freelancer, WordPress Enthusiast, Digital Marketer, Web Developer, Server Operator, Networking Expert. Empowering online presence with diverse skills.

jassweb logo

Jassweb always keeps its services up-to-date with the latest trends in the market, providing its customers all over the world with high-end and easily extensible internet, intranet, and extranet products.

Contact
San Vito Al Tagliamento 33078
Pordenone Italy
Item added to cart.
0 items - 0.00
Open chat
Scan the code
Hello 👋
Can we help you?