Skip to content

Instantly share code, notes, and snippets.

@abidkhan484
Created October 7, 2018 09:38
Show Gist options
  • Select an option

  • Save abidkhan484/18694bb4c57fe1624d7774dddb6784f4 to your computer and use it in GitHub Desktop.

Select an option

Save abidkhan484/18694bb4c57fe1624d7774dddb6784f4 to your computer and use it in GitHub Desktop.
<?php
function getDistanceFromLatLonInKm() {
// $lat1,$lon1,$lat2,$lon2
$lat1 = 23.747047;
$lon1 = 90.386709;
$lat2 = 23.783014;
$lon2 = 90.395310;
$R = 6371; // Radius of the earth in km
$dLat = $this->deg2rad($lat2 - $lat1); // deg2rad below
$dLon = $this->deg2rad($lon2 - $lon1);
$a =
sin($dLat/2) * sin($dLat/2) +
cos($this->deg2rad($lat1)) * cos($this->deg2rad($lat2)) *
sin($dLon/2) * sin($dLon/2)
;
$c = 2 * atan2(sqrt($a), sqrt(1-$a));
$d = $R * $c; // Distance in km
return $d;
}
function deg2rad($deg) {
return $deg * (pi()/180);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment