I am attempting to calculate the distance between two latitude/longitude points. I have a piece of code that mostly works that I yanked from this post but I do not really understand how it works.
Here is the code:
<?php
// POINT 1
$thisLat = deg2rad(44.638);
$thisLong = deg2rad(-63.587);
// POINT 2
$otherLat = deg2rad(44.644);
$otherLong = deg2rad(-63.911);
$MeanRadius = 6378 - 21 * sin($lat1);
$xa = (Cos($thisLat)) * (Cos($thisLong));
$ya = (Cos($thisLat)) * (Sin($thisLong));
$za = (Sin($thisLat));
$xb = (Cos($otherLat)) * (Cos($otherLong));
$yb = (Cos($otherLat)) * (Sin($otherLong));
$zb = (Sin($otherLat));
$distance = $MeanRadius * Acos($xa * $xb + $ya * $yb + $za * $zb);
echo $distance;
?>
I have a couple questions:
- what are xa, ya, za? I understand that they are points on a 3D cartesian plane but where are they relative to? The center of the earth?
- How does this
cos($xa * $xb + $ya * $yb + $za * $zb)
calculate the distance between the points? I know that in 2D I would do this:
Pythagorean Theorem
distance^2 = b^2 + a^2
distance = sqr((y2-y1)^2 + (x2 - x1)^2)
- How accurate will this be? There was some discussion about that on the other page. But I specifically want to use the distance to tell if users are within the something like 10m, 20m or 50m of each other. Will I be able to do this with good accuracy?
- What should I use for
$MeanRadius
? Is that a reasonable value? I think that that value assumes that the earth is a elipse.