32 lines
807 B
PHP
32 lines
807 B
PHP
<?php
|
|
|
|
namespace App\Services;
|
|
|
|
class LocationService
|
|
{
|
|
/**
|
|
* Calculate the distance between two coordinates using the Haversine formula.
|
|
*
|
|
* @param float $lat1
|
|
* @param float $lon1
|
|
* @param float $lat2
|
|
* @param float $lon2
|
|
* @return float Distance in meters
|
|
*/
|
|
public static function calculateDistance($lat1, $lon1, $lat2, $lon2)
|
|
{
|
|
$earthRadius = 6371000; // Radius of Earth in meters
|
|
|
|
$latDelta = deg2rad($lat2 - $lat1);
|
|
$lonDelta = deg2rad($lon2 - $lon1);
|
|
|
|
$a = sin($latDelta / 2) * sin($latDelta / 2) +
|
|
cos(deg2rad($lat1)) * cos(deg2rad($lat2)) *
|
|
sin($lonDelta / 2) * sin($lonDelta / 2);
|
|
|
|
$c = 2 * atan2(sqrt($a), sqrt(1 - $a));
|
|
|
|
return $earthRadius * $c;
|
|
}
|
|
}
|