0

I've got 2 couples of lat/lng coordinates and I would like to get distance between them in meters, but divided in X (east) and Y (north) directions.

I've found this function in a Stack Overflow answer to get distance between 2 points, but I don't know how to separate the North and the East distance:

var rad = function(x) {
  return x * Math.PI / 180;
};

var getDistance = function(p1, p2) {
  var R = 6378137; // Earth’s mean radius in meter
  var dLat = rad(p2.lat() - p1.lat());
  var dLong = rad(p2.lng() - p1.lng());
  var a = Math.sin(dLat / 2) * Math.sin(dLat / 2) +
    Math.cos(rad(p1.lat())) * Math.cos(rad(p2.lat())) *
    Math.sin(dLong / 2) * Math.sin(dLong / 2);
  var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
  var d = R * c;
  return d; // returns the distance in meter
}; 
1

I've found the solution. Here is a javascript function to get the distance divided in east and nord gap:

var getDistance = function(p1, p2) {
    var R = 6371e3; // metres
    var φ1 = rad(p1.lat());
    var φ2 = rad(p2.lat());
    var λ1 = rad(p1.lng());
    var λ2 = rad(p2.lng());

    var x = Math.round((λ2-λ1) * Math.cos((φ1+φ2)/2)*R);
    var y = Math.round((φ2-φ1)*R);

    return {est: x, nord: y};
};

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.