Home > Backend Development > PHP Tutorial > How to Accurately Calculate the Distance Between Two Coordinates in PHP?

How to Accurately Calculate the Distance Between Two Coordinates in PHP?

Susan Sarandon
Release: 2024-12-03 18:47:14
Original
468 people have browsed it

How to Accurately Calculate the Distance Between Two Coordinates in PHP?

Calculating Distance Between Coordinates in PHP

Accurate measurement of distances between coordinates is essential for various applications. One efficient method for calculating this is the Haversine formula, which leverages spherical trigonometry to determine the shortest distance along the surface of a sphere (in this case, the Earth).

Implementing the Haversine Formula in PHP

While the provided PHP implementation attempts to utilize the Haversine formula, it contains certain errors:

  • The delta for longitude should be calculated as $delta_lon = $this->lon_b - $this->lon_a; (the minus sign is reversed).
  • The spherical distance should be calculated as $distance = 2 * $earth_radius * asin(sqrt($a)); (using sin instead of acos).

An Improved Haversine Formula Implementation

Here is a corrected PHP implementation:

class CoordDistance
{
    public $lat_a;
    public $lon_a;
    public $lat_b;
    public $lon_b;

    public $measure_unit = 'kilometers';
    public $measure_state = false;
    public $measure;
    public $error;

    public function DistAB()
    {
        $delta_lat = $this->lat_b - $this->lat_a;
        $delta_lon = $this->lon_b - $this->lon_a;

        $earth_radius = 6372.795477598;

        $alpha = $delta_lat / 2;
        $beta = $delta_lon / 2;
        $a = sin(deg2rad($alpha)) * sin(deg2rad($alpha)) + cos(deg2rad($this->lat_a)) * cos(deg2rad($this->lat_b)) * sin(deg2rad($beta)) * sin(deg2rad($beta));
        $c = asin(min(1, sqrt($a)));
        $distance = 2 * $earth_radius * $c;
        $distance = round($distance, 4);

        $this->measure = $distance;
    }
}
Copy after login

Alternative Method: Vincenty Formula

While the Haversine formula is generally reliable, it can exhibit weaknesses at extreme distances or for antipodal points (located directly opposite each other on the sphere). For these scenarios, the Vincenty formula provides a more accurate solution:

/**
 * Calculates the great-circle distance between two points, using the Vincenty formula.
 *
 * @param float $latitudeFrom Latitude of start point in [deg decimal]
 * @param float $longitudeFrom Longitude of start point in [deg decimal]
 * @param float $latitudeTo Latitude of target point in [deg decimal]
 * @param float $longitudeTo Longitude of target point in [deg decimal]
 * @param float $earthRadius Mean earth radius in [m]
 * @return float Distance between points in [m] (same as earthRadius)
 */
public static function vincentyGreatCircleDistance(
    $latitudeFrom, $longitudeFrom, $latitudeTo, $longitudeTo, $earthRadius = 6371000
) {
    // Convert from degrees to radians
    $latFrom = deg2rad($latitudeFrom);
    $lonFrom = deg2rad($longitudeFrom);
    $latTo = deg2rad($latitudeTo);
    $lonTo = deg2rad($longitudeTo);

    $lonDelta = $lonTo - $lonFrom;
    $a = pow(cos($latTo) * sin($lonDelta), 2) +
        pow(cos($latFrom) * sin($latTo) - sin($latFrom) * cos($latTo) * cos($lonDelta), 2);
    $b = sin($latFrom) * sin($latTo) + cos($latFrom) * cos($latTo) * cos($lonDelta);

    $angle = atan2(sqrt($a), $b);
    return $angle * $earthRadius;
}
Copy after login

The above is the detailed content of How to Accurately Calculate the Distance Between Two Coordinates in PHP?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template