Home Backend Development PHP Tutorial How Can I Accurately Calculate the Distance Between Two Geographical Coordinates in PHP?

How Can I Accurately Calculate the Distance Between Two Geographical Coordinates in PHP?

Dec 08, 2024 pm 08:53 PM

How Can I Accurately Calculate the Distance Between Two Geographical Coordinates in PHP?

Calculating Distance Between Coordinates in PHP

Determining the distance between geographical coordinates is a common task in web development. However, it's crucial to avoid unnecessary API calls and implement efficient calculation methods.

One popular formula for measuring distance is the Haversine formula. It uses the latitude and longitude of two points to calculate the distance between them, taking into account the spherical shape of the Earth.

While you've provided an implementation of the Haversine formula, it may yield unreliable results. To investigate the issue, let's review the formula and identify potential errors:

class CoordDistance
{
    [...]
    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
  • Incorrect Conversion: Ensure that the conversion from degrees to radians is performed correctly using deg2rad().
  • rounding Errors: Take care of potential rounding errors by using high-precision floating-point calculations.
  • Limits: The Haversine formula has limitations when calculating distances for points near the antipodal points (180 degrees apart). In such cases, alternative formulas like the Vincenty formula are more appropriate.

To enhance the reliability of your distance calculations, consider the following suggestions:

Haversine Formula with Improved Precision:

function haversineGreatCircleDistance(float $latitudeFrom, float $longitudeFrom, float $latitudeTo, float $longitudeTo, float $earthRadius = 6371000.0) {
    ...
    $angle = 2 * asin(sqrt(pow(sin($latDelta / 2.0), 2) +
        cos($latFrom) * cos($latTo) * pow(sin($lonDelta / 2.0), 2)));
    return $angle * $earthRadius;
}
Copy after login

Vincenty Formula:

function vincentyGreatCircleDistance(float $latitudeFrom, float $longitudeFrom, float $latitudeTo, float $longitudeTo, float $earthRadius = 6371000.0) {
    ...
    $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

By implementing these improved formulas, you can obtain more accurate distance measurements between coordinates in PHP, even for large distances or points near antipodal points.

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

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

Hot Article

Hot tools Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

11 Best PHP URL Shortener Scripts (Free and Premium) 11 Best PHP URL Shortener Scripts (Free and Premium) Mar 03, 2025 am 10:49 AM

11 Best PHP URL Shortener Scripts (Free and Premium)

Working with Flash Session Data in Laravel Working with Flash Session Data in Laravel Mar 12, 2025 pm 05:08 PM

Working with Flash Session Data in Laravel

6 Extra Skills Every PHP Developer Should Have 6 Extra Skills Every PHP Developer Should Have Feb 28, 2025 am 10:52 AM

6 Extra Skills Every PHP Developer Should Have

Understand Arrays in PHP Understand Arrays in PHP Feb 28, 2025 am 10:53 AM

Understand Arrays in PHP

Build a React App With a Laravel Back End: Part 2, React Build a React App With a Laravel Back End: Part 2, React Mar 04, 2025 am 09:33 AM

Build a React App With a Laravel Back End: Part 2, React

Simplified HTTP Response Mocking in Laravel Tests Simplified HTTP Response Mocking in Laravel Tests Mar 12, 2025 pm 05:09 PM

Simplified HTTP Response Mocking in Laravel Tests

cURL in PHP: How to Use the PHP cURL Extension in REST APIs cURL in PHP: How to Use the PHP cURL Extension in REST APIs Mar 14, 2025 am 11:42 AM

cURL in PHP: How to Use the PHP cURL Extension in REST APIs

12 Best PHP Chat Scripts on CodeCanyon 12 Best PHP Chat Scripts on CodeCanyon Mar 13, 2025 pm 12:08 PM

12 Best PHP Chat Scripts on CodeCanyon

See all articles