-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlocation.js
36 lines (29 loc) · 996 Bytes
/
location.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
//real time location update
export function regionFrom(lat, lon, accuracy) {
const oneDegreeOfLongitudeInMeters = 111.32 * 1000;
const circumference = (40075 / 360) * 1000;
const latDelta = accuracy * (1 / (Math.cos(lat) * circumference));
const lonDelta = (accuracy / oneDegreeOfLongitudeInMeters);
return {
latitude: lat,
longitude: lon,
latitudeDelta: Math.max(0, latDelta),
longitudeDelta: Math.max(0, lonDelta)
};
}
export function getLatLonDiffInMeters(lat1, lon1, lat2, lon2) {
var R = 6371; // radius of the earth in km
var dLat = deg2rad(lat2-lat1); // deg2rad below
var dLon = deg2rad(lon2-lon1);
var a =
Math.sin(dLat/2) * Math.sin(dLat/2) +
Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) *
Math.sin(dLon/2) * Math.sin(dLon/2)
;
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
var d = R * c; // distance in km is calculated
return d * 1000;
}
function deg2rad(deg) {
return deg * (Math.PI/180)
}