speedometer/main.js

36 lines
723 B
JavaScript
Raw Normal View History

2019-02-21 17:41:33 -07:00
'use strict';
2019-02-21 18:07:06 -07:00
const speedDiv = document.getElementById('speed')
2019-02-21 17:41:33 -07:00
// Check for API
if (!'geolocation' in navigator) {
alert('No geolocation API available')
2019-02-21 18:07:06 -07:00
speedDiv.innerHTML = 'No API'
2019-02-21 17:41:33 -07:00
} else {
2019-02-21 18:07:06 -07:00
const WID = navigator.geolocation.watchPosition(
// Success
function (loc) {
if (loc.coords.speed) {
console.log('Got speed:',loc.coords.speed)
// convert to mph and display
speedDiv.innerHTML = (2.23693629205*loc.coords.speed).toFixed(1)
} else {
speedDiv.innerHTML = '0.0'
}
},
// Error
function() {
console.error('Could not determine GPS position')
},
// Options
{
enableHighAccuracy: true,
2019-02-21 18:07:06 -07:00
}
)
2019-02-21 17:41:33 -07:00
}