-
Notifications
You must be signed in to change notification settings - Fork 0
/
speed.js
78 lines (66 loc) · 1.73 KB
/
speed.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
const axios = require('axios')
const fs = require('fs')
var positions = []
const ENDPOINT = "https://www.googleapis.com/geolocation/v1/geolocate?key=" + process.env.GEOLOCATION_API_KEY;
const INTERVAL = 1000;
function getLocation() {
axios
.post(ENDPOINT)
.then(res => {
// console.log(`statusCode: ${res.status}`)
console.log(res.data)
return res.data
})
.catch(error => {
console.error(error)
return null;
})
}
function getDistance(loc1, loc2) {
const R = 6371e3;
const phi1 = lat1 * Math.PI/180;
const phi2 = lat2 * Math.PI/180;
const dphi = (loc2.lat-loc1.lat) * Math.PI/180;
const dgamma = (loc2.lng-loc1.lng) * Math.PI/180;
const a = Math.sin(dgamma/2) * Math.sin(dphi/2) +
Math.cos(phi1) * Math.cos(phi2) *
Math.sin(dgamma/2) * Math.sin(dgama/2);
const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
const d = R * c;
return d;
}
function toMPH(mpms) {
return mpms/0.00044704;
}
function getSpeed(d) {
return d/INTERVAL;
}
function locationTracking() {
positions = []
c = setInterval(function() {
loc = getLocation()
if(loc) positions.push(loc);
}, INTERVAL);
}
function stopLocationTracking(c) {
clearInterval(c);
}
function positionsToSpeeds(pos) {
s = []
for(var i = 1; i < pos.length-1; i++) {
s.append(toMPH((pos[i]-pos[i-1])/INTERVAL))
}
return s
}
function writeSpeeds(s, id="0") {
var content = "speed\n"
for(var i = 0; i < s.length; i++) {
content += s[i].toString() + "\n"
}
fs.writeFile('./data/speed_' + id + ".csv", content, err => {
if (err) {
console.error(err)
return
}
})
}