-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
22 lines (16 loc) · 761 Bytes
/
Copy pathindex.js
File metadata and controls
22 lines (16 loc) · 761 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
'use strict';
const earthRadiusAtGeodeticLatitude = require('earth-radius-at-geodetic-latitude');
const toRadians = degrees => degrees * (Math.PI / 180);
function haversineDistance(start, end) {
const r_meters = earthRadiusAtGeodeticLatitude(start.latitude);
const d_lat = toRadians(end.latitude - start.latitude);
const d_lon = toRadians(end.longitude - start.longitude);
const start_lat = toRadians(start.latitude);
const end_lat = toRadians(end.latitude);
const a = Math.sin(d_lat / 2) * Math.sin(d_lat / 2) +
Math.sin(d_lon / 2) * Math.sin(d_lon / 2) *
Math.cos(start_lat) * Math.cos(end_lat);
const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
return r_meters * c;
}
module.exports = haversineDistance;