Hi,
I think that this was certainly already done, then I ask.
what is the way to calculate the distance between 2 positions (between 2 couples of lat,lon)
thanks
I found this a while ago and grabbed it.
function distance(lat1, lon1, lat2, lon2)
{
var x = deg2rad((lon2 - lon1)) * Math.cos(deg2rad( (lat1 + lat2) / 2));
var y = deg2rad(lat2 - lat1);
var distance = Math.sqrt(x * x + y * y) * R;
return distance.format("%d");
}
Dave.
As an example, at a latitude of 40 degrees (north or south) the Pythagorean formula will give distances roughly 30% too large when measuring distance in an east/west direction.
The cosine of average latitude is being multiplied by the delta-lat computed in the example, which compensates for this error. This compensation is also an approximation, but still one that will be plenty accurate over short distances. This approximation should be sufficient for almost all applications when measuring distances less than a few kilometers.