From d7a0b9a83de87d9987e1274d1974cc93510ea590 Mon Sep 17 00:00:00 2001 From: Keith Irwin Date: Thu, 21 Feb 2019 21:00:05 -0500 Subject: [PATCH] Added error handling and geolocation options --- main.js | 32 +++++++++++++++++++++++--------- 1 file changed, 23 insertions(+), 9 deletions(-) diff --git a/main.js b/main.js index f66b44e..4f5aff1 100644 --- a/main.js +++ b/main.js @@ -8,14 +8,28 @@ if (!'geolocation' in navigator) { speedDiv.innerHTML = 'No API' } else { - const WID = navigator.geolocation.watchPosition( function (loc) { - // got speed - 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' + 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, } - }) + + ) }