git ssb

0+

Dominic / %oZNumvaRdlZDV/jsRcA…



Tree: 461be9d29ffa9ae1336651570c5ac4d45c8baa3a

Files: 461be9d29ffa9ae1336651570c5ac4d45c8baa3a / index.js

2054 bytesRaw
1var GreatCircle = require('great-circle')
2
3var pre = document.createElement('pre')
4var speed = document.createElement('h1')
5var position = document.createElement('h3')
6document.body.appendChild(speed)
7document.body.appendChild(position)
8document.body.appendChild(pre)
9
10function flatten (p) {
11 var o = {}
12 for(var k in p)
13 if(p[k] && 'object' === typeof p[k])
14 o[k] = flatten(p[k])
15 else
16 o[k] = p[k]
17 return o
18}
19
20var DEGREE_SYMBOL = '\u00b0'
21
22
23function decimalTudeToMinutes (tude) {
24 return ~~tude.toString()+DEGREE_SYMBOL+Math.abs((tude - ~~(tude))*60).toPrecision(4)
25}
26
27var positions = []
28
29pre.textContent = 'waiting for position...'
30
31function round(r, n) {
32 var f = Math.pow(10, n)
33 return Math.round(r*f)/f
34}
35
36navigator.geolocation.watchPosition(function (e) {
37 positions.push(flatten(e))
38 //keep just one minute's worth of locations.
39
40 while(positions.length && positions[0].timestamp < Date.now() - 60e3) // one minute
41 positions.shift()
42
43 var lat = e.coords.latitude, long = e.coords.longitude
44 var movement = positions.map(function (_e) {
45 var _lat = _e.coords.latitude, _long = _e.coords.longitude
46 var time = e.timestamp - _e.timestamp
47 return {
48 distance: GreatCircle.distance(_lat, _long, lat, long, 'NM'),
49 heading: GreatCircle.bearing(_lat, _long, lat, long),
50 speed: GreatCircle.distance(_lat, _long, lat, long, 'NM') / (time / (1000*60*60)),
51 time: (e.timestamp - _e.timestamp)/1000
52 }
53 })
54
55 position.textContent = (
56 decimalTudeToMinutes(e.coords.latitude)
57 + ', ' +
58 decimalTudeToMinutes(e.coords.longitude)
59 )
60
61 E = e
62 var metersPerSecond = e.coords.speed || 0
63 var metersPerHour = metersPerSecond*3600
64 var metersPerNauticalMile = 1852.001
65 var knots = metersPerHour/metersPerNauticalMile
66 speed.textContent = round(knots, 2)
67 pre.textContent = JSON.stringify(movement, null, 2)
68}, function (err) {
69 pre.textContent = JSON.stringify({error:err.code, message: err.message}, null, 2)
70}, {
71 enableHighAccuracy: true,
72 timeout: 5000,
73 maximumAge: 0
74})
75
76

Built with git-ssb-web