How to Get Latitude and Longitude From Address in Codeigniter Using Google Map
To get Latitude and Longitude From Address in Codeigniter using the google geocode API:
Use google API with key
Controller File:
public function getlocation() { $address = "London united state"; $array = $this->get_longitude_latitude_from_adress($address); print_r($array); // Display complete response $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 ]; }c
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 the address.
OUTPUT:
Array ( [lat] => 16.1738766 [lng] => 81.1382224 )