git ssb

0+

Dominic / %oZNumvaRdlZDV/jsRcA…



Tree: 8d95a74c4cf627c9fb88db02d4c53a97747b4127

Files: 8d95a74c4cf627c9fb88db02d4c53a97747b4127 / index.js

993 bytesRaw
1var GreatCircle = require('great-circle')
2
3var pre = document.createElement('pre')
4document.body.appendChild(pre)
5
6function flatten (p) {
7 var o = {}
8 for(var k in p)
9 if(p[k] && 'object' === typeof p[k])
10 o[k] = flatten(p[k])
11 else
12 o[k] = p[k]
13 return o
14}
15
16var positions = []
17
18pre.textContent = 'waiting for position...'
19
20navigator.geolocation.watchPosition(function (e) {
21 console.log(e.coords, e.timestamp)
22 positions.push(flatten(e))
23 //keep just one minute's worth of locations.
24 if(e.timestamp < Date.now() - 60e3) // one minute
25 positions.shift()
26
27 var lat = e.coords.latitude, log = e.coords.longitude
28 var movement = positions.map(function (_e) {
29 var _lat = _e.coords.latitude, _long = _e.coords.longitude
30 return {
31 distance: GreatCircle.distance(_lat, _long, lat, long, 'NM'),
32 heading: GreatCircle.bearing(_lat, _long, lat, long)
33 }
34 })
35
36 pre.textContent = JSON.stringify({current: flatten(e), movement: movement}, null, 2)
37})
38
39
40
41

Built with git-ssb-web