From 385f0ae53529ab45a627e84fca468ecf0d9fdd93 Mon Sep 17 00:00:00 2001 From: Skylar Ittner Date: Thu, 26 Sep 2019 10:01:20 -0600 Subject: [PATCH] Use last known location before GPS warms up (close #19) --- www/assets/js/location.js | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/www/assets/js/location.js b/www/assets/js/location.js index a0ed3b6..13ddfdf 100644 --- a/www/assets/js/location.js +++ b/www/assets/js/location.js @@ -14,6 +14,12 @@ var userPosition = { } }; +// Preload last known location while GPS warms up +if (localStorage.getItem("user_latitude") != null && localStorage.getItem("user_longitude") != null) { + userPosition.coords.latitude = localStorage.getItem("user_latitude"); + userPosition.coords.longitude = localStorage.getItem("user_longitude"); +} + var geoerrorcount = 0; var mapLocationControlStarted = false; @@ -21,6 +27,8 @@ var mapLocationControlStarted = false; if ("geolocation" in navigator) { navigator.geolocation.watchPosition(function (position) { userPosition = position; + localStorage.setItem("user_latitude", userPosition.coords.latitude); + localStorage.setItem("user_longitude", userPosition.coords.longitude); if (mapLocationControlStarted) { //setMapLocation(position.coords.latitude, position.coords.longitude); // Don't refresh at an interval less than ten seconds @@ -160,14 +168,14 @@ function getDisplayDistance(meters, space) { space = true; } var units = localStorage.getItem("units"); - + if (units == null) { units = "metric"; } - + var number = Math.round(meters); var label = "m"; - + if (units == "imperial") { // Convert to feet number = Math.round(number * 3.28084); @@ -192,7 +200,7 @@ function getDisplayDistance(meters, space) { label = "km"; } } - + if (space) { return number + " " + label; }