calculate distance between two points in php

Asked

Viewed 563 times

-2

I am doing a project and I need to calculate the distance in kilometers(KM) using the latitude and longitude I found a function in php but when I will take the result and compare with the google it of different, the function I found was this:

    function calcDist($lat_A, $long_A, $lat_B, $long_B) {

    $distance = sin(deg2rad($lat_A))
        * sin(deg2rad($lat_B))
        + cos(deg2rad($lat_A))
        * cos(deg2rad($lat_B))
        * cos(deg2rad($long_A - $long_B));

    $distance = (rad2deg(acos($distance))) * 69.09;

    return $distance;
}

The function returns me :

0.15712674480811772

But the right distance would be :

260 meters

  • Your formula is wrong, you are trying to apply the law of sines in geodetic calculus. This formula works well in a plane but in a spheroid it does not work I know two formulas to calculate the geographical distance to Haversine and The Geodesic Projection. Haversine is more precise but has to come up with differential corrections for earth distances due to the earth’s radius variations so I did this example using geodetic projection with 15% correction factor. I did not put as a response due to the inherent inaccuracy of the formula

1 answer

1


if (!function_exists('distancia')) {
    function distancia($lat1, $lon1, $lat2, $lon2)
    {

        $lat1 = deg2rad($lat1);
        $lat2 = deg2rad($lat2);
        $lon1 = deg2rad($lon1);
        $lon2 = deg2rad($lon2);

        $dist = (6371 * acos(cos($lat1) * cos($lat2) * cos($lon2 - $lon1) + sin($lat1) * sin($lat2)));
        $dist = number_format($dist, 3, '.', '');
        return $dist;
    }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.