How to obtain the distance between two Location objects

Hello,
I've been searching on the forum but I didn't find the answer: can you help me on how to obtain the distance between two Location objects please?

Thank you!
Regards,

Marc
  • If you know you're only going to deal with points that are relatively close, you can use the pythagorean theorem. If you are potentially showing the distance between points very far away, you should consider using the haversine. This post has examples of both bits of code.
  • Yes, the thread Travis linked to provides both methods. I've been using the "planetscooter" method since then, but I changed it some parameter wise and this is it now:

    //
    // distance between two location (meters) and returns formatted string
    //
    function computeDistance(lat1, lon1, lat2, lon2) {
    var mlat,mlon;
    var dx, dy, distance;

    mlat = (lat1 + lat2) / 2 * 0.01745;
    dx = 111.3 * Math.cos(mlat) * (lon1 - lon2);
    dy = 111.3 * (lat1 - lat2);
    distance = 1000 * Math.sqrt(dx * dx + dy * dy);
    return distToStr(distance);
    }


    it isn't exact for short distances (<300'), and some of that is the GPS locations can be a bit off, just by the nature of GPS. "distToStr()" converts the meters into a string of meters or feet, km or miles, based on the watch's setting for "distanceUnits".
  • Thank you very much.
    This is a good starting point :)