Tisamu
3/5/2018 - 8:59 AM

Distance between two positions (lon/lat)

Calculate the position between two given positions by their Longitude & Latitude

function toRad(Value) {
  /** Converts numeric degrees to radians */
  return Value * Math.PI / 180;
}

let earth_radius = 6371e3; /* Earth radius in meters */
let lat1 = 50.5144738; /* Given first Latitude */
let lon1 = 1.6190026; /* Given first Longitude */

let lat2;
let lon2;

lat2 = 50.7778815; /* Given second Latitude */
lon2 = 1.6112428; /* Given first Longitude */

let deltaLat = toRad((lat2 - lat1));
let deltaLon = toRad((lon2 - lon1));

/* haversine Formula to calculate distance between two positions */
let a = Math.sin(deltaLat / 2) * Math.sin(deltaLat / 2) +
  Math.cos(lat1) * Math.cos(lat2) *
  Math.sin(deltaLon / 2) * Math.sin(deltaLon / 2);

let c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));

let d = earth_radius * c;

return d