function distance(float $lat1, float $lon1, float $lat2, float $lon2, string $unit): float
{
$theta = $lon1 - $lon2;
$dist = sin(deg2rad($lat1)) * sin(deg2rad($lat2)) + cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($theta));
$dist = acos($dist);
$dist = rad2deg($dist);
$miles = $dist * 60 * 1.1515;
$unit = strtoupper($unit);
switch ($unit) {
case 'M':
return $miles;
case 'K':
return $miles * 1.609344;
case 'N':
return $miles * 0.8684;
}
}
// Rotterdam
$lat1 = 51.924419;
$lon1 = 4.47917;
// Amsterdam
$lat2 = 52.370216;
$lon2 = 4.477733;
Miles
distance($lat1, $lon1, $lat2, $lon2, 'M'); // 30.800174981173
Kilometers
distance($lat1, $lon1, $lat2, $lon2, 'K'); // 49.568076804901
Nautical Miles
distance($lat1, $lon1, $lat2, $lon2, 'N'); // 26.746871953651
Categorieën
PHP