Distance between 2 positions

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.
  • +
    R = 6371; // Radius of the earth
    I think
    and distance in meters I guess too ?!
  • 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.


    For the record, this is formula is known as equirectangular approximation and is basically just applying Pythagoras’ theorem. It is computationally quick but will not be as accurate, especially over long distances (hence why it's called an 'approximation') or in other specific cases. The inaccuracy may be statistically unimportant for small distances (less than a few kilometres) but the inaccuracy will also change based on the where the two points are located. 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. If you want the actual (ie. correct) distance in all cases then you will want to look at using the haversine formula.

    Here's a good article about the errors involved when calculating distances on a sphere using the two formulas: http://gis.stackexchange.com/questions/58653/what-is-the-approximate-error-of-the-pythagorean-theorem-vs-haversine-formula-i

    Cheers,
    Douglas
  • That is the approximate radius of the Earth in kilometers.
  • Ok, then, I need to put R = 6371000; in the formula then ?!
  • Former Member
    Former Member over 8 years ago
    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.
  • 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.


    How right you are! I missed the cosign and got distracted by the error amounts found when using Pythagorean formula.

    Still, my comment about it being an approximation remains -- though for small distances the error in the approximation is statistically insignificant. Yay math! :)

    Cheers,
    Douglas
  • If you want to get more accurate over long distances, have a look at the algorithms here: http://www.movable-type.co.uk/scripts/latlong.html. They are easy enough to convert to MonkeyC.