From de2acf34f72c5db7355d0c6b4799abd0bb629464 Mon Sep 17 00:00:00 2001 From: Skylar Ittner Date: Thu, 18 Feb 2021 23:17:12 -0700 Subject: [PATCH] Start watching position after finish loading platform code --- www/assets/js/location.js | 122 +++++++++++++++++++------------------- www/assets/js/platform.js | 12 ++-- 2 files changed, 70 insertions(+), 64 deletions(-) diff --git a/www/assets/js/location.js b/www/assets/js/location.js index 6b5f8f9..4a81154 100644 --- a/www/assets/js/location.js +++ b/www/assets/js/location.js @@ -41,77 +41,79 @@ var geoerrorcount = 0; var mapLocationControlStarted = false; -if ("geolocation" in navigator) { - navigator.geolocation.watchPosition(function (position) { - userPosition.coords = position.coords; - userPosition.updated = time(); - setStorage("user_latitude", userPosition.coords.latitude); - setStorage("user_longitude", userPosition.coords.longitude); - if (mapLocationControlStarted) { - // Don't refresh at an interval less than ten seconds - var currentTimestamp = Math.floor(Date.now() / 1000); - if (lastGpsUpdateTimestamp < (currentTimestamp - 10)) { - updateDistances(position.coords.latitude, position.coords.longitude); - - var alertinterval = getStorage("alertinterval"); - if (alertinterval == null) { - alertinterval = 30; - } else { - alertinterval = alertinterval * 1; - } +function setupPositionWatcher() { + if ("geolocation" in navigator) { + navigator.geolocation.watchPosition(function (position) { + userPosition.coords = position.coords; + userPosition.updated = time(); + setStorage("user_latitude", userPosition.coords.latitude); + setStorage("user_longitude", userPosition.coords.longitude); + if (mapLocationControlStarted) { + // Don't refresh at an interval less than ten seconds + var currentTimestamp = Math.floor(Date.now() / 1000); + if (lastGpsUpdateTimestamp < (currentTimestamp - 10)) { + updateDistances(position.coords.latitude, position.coords.longitude); + + var alertinterval = getStorage("alertinterval"); + if (alertinterval == null) { + alertinterval = 30; + } else { + alertinterval = alertinterval * 1; + } - lastGpsUpdateTimestamp = currentTimestamp; - for (var i = 0; i < packages.length; i++) { - if (packages[i].distance * 1 < getStorage("alertradius") * 1) { + lastGpsUpdateTimestamp = currentTimestamp; + for (var i = 0; i < packages.length; i++) { + if (packages[i].distance * 1 < getStorage("alertradius") * 1) { - if (packages[i].lastAlert > currentTimestamp - alertinterval) { - continue; - } + if (packages[i].lastAlert > currentTimestamp - alertinterval) { + continue; + } - if (getUndeliveredCount(packages[i]) == 0) { - continue; - } + if (getUndeliveredCount(packages[i]) == 0) { + continue; + } - try { - if (map != null) { - map.updatePackageLayer(packages); + try { + if (map != null) { + map.updatePackageLayer(packages); + } + loadPackageList(); + } catch (ex) { + // It'll show up sooner or later anyways } - loadPackageList(); - } catch (ex) { - // It'll show up sooner or later anyways - } - playSound("alert"); - packages[i].lastAlert = currentTimestamp; + playSound("alert"); + packages[i].lastAlert = currentTimestamp; + } } } + } else { + if (map != null) { + map.startLocateControl(); + mapLocationControlStarted = true; + } } - } else { - if (map != null) { - map.startLocateControl(); - mapLocationControlStarted = true; + }, function (err) { + if (typeof error == "function") { + error(err.message); } - } - }, function (err) { - if (typeof error == "function") { - error(err.message); - } - }, { - enableHighAccuracy: true, - timeout: 5000, - maximumAge: 0 - }); -} else { - geoerrorcount++; - console.log("Warn", "Geolocation error #" + geoerrorcount + ": ", error); - // Stop showing error toasts if they're happening a lot - if (geoerrorcount <= 3) { - app.toast.show({ - text: ' ' + error, - position: "bottom", - destroyOnClose: true, - closeTimeout: 1000 * 3 + }, { + enableHighAccuracy: true, + timeout: 5000, + maximumAge: 0 }); + } else { + geoerrorcount++; + console.log("Warn", "Geolocation error #" + geoerrorcount + ": ", error); + // Stop showing error toasts if they're happening a lot + if (geoerrorcount <= 3) { + app.toast.show({ + text: ' ' + error, + position: "bottom", + destroyOnClose: true, + closeTimeout: 1000 * 3 + }); + } } } diff --git a/www/assets/js/platform.js b/www/assets/js/platform.js index 72b63c5..ed19589 100644 --- a/www/assets/js/platform.js +++ b/www/assets/js/platform.js @@ -169,12 +169,12 @@ function initCordova() { }); } -// if (getStorage("runinbackground") == "true") { -// cordova.plugins.foregroundService.start('PackageHelper', 'GPS active'); -// } - // Make sure the status bar color is set properly applyColorTheme(); + + // start watching user position after everything's done, hopefully this will make it work + // with ios + setupPositionWatcher(); }, false); openBrowser = function (url) { cordova.InAppBrowser.open(url, '_blank', 'location=yes'); @@ -266,6 +266,8 @@ function initNW() { require('nw.gui').Shell.openExternal($(this).attr("href")); evt.preventDefault(); }); + + setupPositionWatcher(); } function initBrowser() { @@ -285,6 +287,8 @@ function initBrowser() { window.open($(this).attr("href"), "_blank"); evt.preventDefault(); }); + + setupPositionWatcher(); } function initPlatform() {