kalkulator kilometrów na podstawie długości i szerokosci geograficznej

class DistanceCalculator
{
    private const EARTH_RADIUS = 6371.009;

    public function calculate(CoordinableInterface $from, CoordinableInterface $to): float
    {
        return $this->calculateBetweenPoints((float)$from->getLat(), (float)$from->getLng(), (float)$to->getLat(), (float)$to->getLng());
    }

    public function calculateBetweenPoints(
        float $latitudeFrom,
        float $longitudeFrom,
        float $latitudeTo,
        float $longitudeTo
    ): float {
        $latFrom = deg2rad($latitudeFrom);
        $lonFrom = deg2rad($longitudeFrom);
        $latTo = deg2rad($latitudeTo);
        $lonTo = deg2rad($longitudeTo);

        $latDelta = $latTo - $latFrom;
        $lonDelta = $lonTo - $lonFrom;

        $angle = 2 * asin(sqrt(pow(sin($latDelta / 2), 2) + cos($latFrom) * cos($latTo) * pow(sin($lonDelta / 2), 2)));

        return $angle * static::EARTH_RADIUS;
    }
}
Komentarze wyłączone