Get Latitude and Longitude From Address in Codeigniter 4

08-Aug-2020

.

Admin

Hi Guys,

In this blog, I will show you how to get latitude and longitude from address in codeigniter 4. We will learn codeigniter 4 get latitude abd longitude from address. Get latitude and longitude from address in Codeigniter.

In this article, I have to find out the latitude and longitude of the place from address. PHP Code Igniter library class to get latitude & longitude, get address for specified latitude & longitude, get direction from one place to another place.

Here I will give you example for get latitude and longitude from address in codeigniter. So let's see bellow example.

Example


public function getlocation()

{

$address = "Mumabi India";

$array = $this->getLongitudeLatitude($address);

$latitude = round($array['latitude'], 6);

$longitude = round($array['longitude'], 6);

}

function getLongitudeLatitude($address){

$latitude = 0;

$longitude = 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)){

$latitude = ($json1->{'results'}[0]->{'geometry'}->{'location'}->{'latitude'});

$long = ($json1->{'results'}[0]->{'geometry'}->{'location'}->{'longitude'});

}

} catch(exception $e) { }

return [

'latitude' => $latitude,

'longitude' => $long

];

}

It will help you...

#Codeigniter