Added error handling and geolocation options
parent
9dd57845a8
commit
d7a0b9a83d
32
main.js
32
main.js
|
@ -8,14 +8,28 @@ if (!'geolocation' in navigator) {
|
||||||
speedDiv.innerHTML = 'No API'
|
speedDiv.innerHTML = 'No API'
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
const WID = navigator.geolocation.watchPosition( function (loc) {
|
const WID = navigator.geolocation.watchPosition(
|
||||||
// got speed
|
|
||||||
if (loc.coords.speed) {
|
// Success
|
||||||
console.log('Got speed:',loc.coords.speed)
|
function (loc) {
|
||||||
// convert to mph and display
|
if (loc.coords.speed) {
|
||||||
speedDiv.innerHTML = (2.23693629205*loc.coords.speed).toFixed(1)
|
console.log('Got speed:',loc.coords.speed)
|
||||||
} else {
|
// convert to mph and display
|
||||||
speedDiv.innerHTML = '0.0'
|
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,
|
||||||
}
|
}
|
||||||
})
|
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue