-
Notifications
You must be signed in to change notification settings - Fork 0
/
haversine.js
42 lines (38 loc) · 955 Bytes
/
haversine.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
const cos = Math.cos;
const sin = Math.sin;
const sqrt = Math.sqrt;
const PI = Math.PI;
// Earth radius in diff metrics
const radii = {
km: 6371,
mile: 3960,
meter: 6371000,
nmi: 3440
};
const haversine = {
getLatitudeBounds(lat, d, metric) {
if (!metric) {
metric = "meter";
}
(arguments.length === 0) && (() => { throw new Error('Please fill in all arguments'); })();
let metricPerDeg = (2*PI/360) * radii[metric];
let degChange = d/metricPerDeg;
return {
lowerLat: lat - degChange,
upperLat: lat + degChange
};
},
getLongitudeBounds(lat, long, d, metric) {
if (!metric) {
metric = "meter";
}
(arguments.length === 0) && (() => { throw new Error('Please fill in all arguments'); })();
let metricPerDeg = (2*PI/360) * radii[metric] * cos(lat * PI/180);
let degChange = d/metricPerDeg;
return {
lowerLong: long - degChange,
upperLong: long + degChange
};
}
};
module.exports = haversine;