-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
– Augusto Vasques