Skip to content

Instantly share code, notes, and snippets.

@ivalkenburg
Created October 10, 2019 15:29
Show Gist options
  • Save ivalkenburg/cd5fb9347d92d9f4a93c9ee3652b13f6 to your computer and use it in GitHub Desktop.
Save ivalkenburg/cd5fb9347d92d9f4a93c9ee3652b13f6 to your computer and use it in GitHub Desktop.
const distance = function(point1, point2) {
const R = 6371; // Radius of the earth in km
const dLat = (point2.lat - point1.lat) * Math.PI / 180;
const dLong = (point2.long - point1.long) * Math.PI / 180;
let a = 0.5 - Math.cos(dLat) / 2 + Math.cos(point1.lat * Math.PI / 180) * Math.cos(point2.lat * Math.PI / 180) * (1 - Math.cos(dLong)) / 2;
return R * 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
}
const me = {long: 11.19180679321289, lat: 2.881151180400001};
const points = [
{long: 11.198158264160156, lat: 2.881151180400001}, // 1km
{long: 11.214981079101562, lat: 2.8823512904823567}, // 3km
{long: 76.2890625, lat: 50.17689812200107},
];
const results = points.map(point => {
return distance(me, point);
})
console.log(results);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment