Calculate distance between two positions

Hello together,

I'm currently trying to calculate the distance between two positions in a widget. I've found this javaScript algorithm to do so but am not really sure how to translate it to MC:

var R = 6371000; // metres
var φ1 = lat1.toRadians();
var φ2 = lat2.toRadians();
var Δφ = (lat2-lat1).toRadians();
var Δλ = (lon2-lon1).toRadians();

var a = Math.sin(Δφ/2) * Math.sin(Δφ/2) +
Math.cos(φ1) * Math.cos(φ2) *
Math.sin(Δλ/2) * Math.sin(Δλ/2);
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));

var d = R * c;

The problem is these two lines:

var Δφ = (lat2-lat1).toRadians();
var Δλ = (lon2-lon1).toRadians();

since I dont know how to convert the two subtracted degrees to radians.

Any help would be appreciated.
  • Former Member
    Former Member over 9 years ago
    This is what I do in my App:

    // computes the direct distance between two points
    // distance = sqrt(dx * dx + dy * dy)
    // with distance: in km
    // dx = 111.3 * cos(lat) * (lon1 - lon2)
    // lat = (lat1 + lat2) / 2 * 0.01745
    // dy = 111.3 * (lat1 - lat2)
    // lat1, lat2, lon1, lon2: Breite, Länge in Grad
    // 1° = π/180 rad ≈ 0.01745
    function computeDistance (pos1, pos2) {
    var lat1, lat2, lon1, lon2, lat, lon;
    var dx, dy, distance;

    lat1 = pos1.toDegrees()[0].toFloat();
    lon1 = pos1.toDegrees()[1].toFloat();
    lat2 = pos2.toDegrees()[0].toFloat();
    lon2 = pos2.toDegrees()[1].toFloat();

    lat = (lat1 + lat2) / 2 * 0.01745;
    dx = 111.3 * Math.cos(lat) * (lon1 - lon2);
    dy = 111.3 * (lat1 - lat2);
    distance = 1000 * Math.sqrt(dx * dx + dy * dy);
    distance = distance.toNumber();
    strokelength = Lang.format("$1$ m", [distance]);
    }




    And call it like

    if( Toybox has :Position ) {
    loc_info = Position.getInfo();
    if (loc_info != null) {
    if (validstrokeposition == true) {
    thisstrokeat = loc_info.position;
    computeDistance(laststrokeat, thisstrokeat);
    }
    }
    validstrokeposition = true;
    laststrokeat = loc_info.position;

    }
  • The problem is these two lines:

    var Δφ = (lat2-lat1).toRadians();
    var Δλ = (lon2-lon1).toRadians();

    since I dont know how to convert the two subtracted degrees to radians.


    If you have Position.Location object, it has a toRadians(). Most likely you'll have a Location object to start, so this isn't an issue. If you have the latitude and longitude in degrees, you just need to convert from degrees to radians.

    function degrees_to_radians(deg) {
    return deg * Math.PI / 180;
    }


    The conversion looks like...

    function Geodetic_distance_deg(lat1, lon1, lat2, lon2) {
    lat1 = degrees_to_radians(lat1);
    lon1 = degrees_to_radians(lon1);
    lat2 = degrees_to_radians(lat2);
    lon2 = degrees_to_radians(lon2);

    return Geodetic_distance_rad(lat1, lon1, lat2, lon2)
    }

    function Geodetic_distance_rad(lat1, lon1, lat2, lon2) {
    var dy = (lat2-lat1);
    var dx = (lon2-lon1);

    var sy = Math.sin(dy / 2);
    sy *= sy;

    var sx = Math.sin(dx / 2);
    sx *= sx;

    var a = sy + Math.cos(lat1) * Math.cos(lat2) * sx;

    // you'll have to implement atan2
    var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));

    var R = 6371000; // radius of earth in meters
    return R * c;
    }
  • Thanks for the answers. Helped me a lot.