Skip to content

Instantly share code, notes, and snippets.

@albulescu
Last active March 9, 2023 03:49
Show Gist options
  • Save albulescu/eba36e571ba73af33d4b30d472272f88 to your computer and use it in GitHub Desktop.
Save albulescu/eba36e571ba73af33d4b30d472272f88 to your computer and use it in GitHub Desktop.
Distance between two coordinates
function distance(lon1, lat1, lon2, lat2) {
const R = 6371e3; // metres
const Q1 = lat1 * Math.PI / 180;
const Q2 = lat2 * Math.PI / 180;
const DP = (lat2 - lat1) * Math.PI / 180;
const DA = (lon2 - lon1) * Math.PI / 180;
const a = Math.sin(DP / 2) * Math.sin(DP / 2) +
Math.cos(Q1) * Math.cos(Q2) *
Math.sin(DA / 2) * Math.sin(DA / 2);
const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
const d = R * c; // in metres
return d
}
function walkingEta(d) {
var hrsdec = d / 1000 / 5.1
var hrs = Math.floor(hrsdec);
var minsdec = (hrsdec - hrs) * 60;
return Math.round(minsdec);
}
const d = distance(21.230097, 45.758419, 21.229034, 45.757978);
const mins = walkingEta(d);
console.log(d + " meters distance")
console.log(mins + " minutes walking time")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment