Start watching position after finish loading platform code

Skylar Ittner 3 years ago
parent ad869d1b97
commit de2acf34f7

@ -41,77 +41,79 @@ var geoerrorcount = 0;
var mapLocationControlStarted = false; var mapLocationControlStarted = false;
if ("geolocation" in navigator) { function setupPositionWatcher() {
navigator.geolocation.watchPosition(function (position) { if ("geolocation" in navigator) {
userPosition.coords = position.coords; navigator.geolocation.watchPosition(function (position) {
userPosition.updated = time(); userPosition.coords = position.coords;
setStorage("user_latitude", userPosition.coords.latitude); userPosition.updated = time();
setStorage("user_longitude", userPosition.coords.longitude); setStorage("user_latitude", userPosition.coords.latitude);
if (mapLocationControlStarted) { setStorage("user_longitude", userPosition.coords.longitude);
// Don't refresh at an interval less than ten seconds if (mapLocationControlStarted) {
var currentTimestamp = Math.floor(Date.now() / 1000); // Don't refresh at an interval less than ten seconds
if (lastGpsUpdateTimestamp < (currentTimestamp - 10)) { var currentTimestamp = Math.floor(Date.now() / 1000);
updateDistances(position.coords.latitude, position.coords.longitude); if (lastGpsUpdateTimestamp < (currentTimestamp - 10)) {
updateDistances(position.coords.latitude, position.coords.longitude);
var alertinterval = getStorage("alertinterval");
if (alertinterval == null) { var alertinterval = getStorage("alertinterval");
alertinterval = 30; if (alertinterval == null) {
} else { alertinterval = 30;
alertinterval = alertinterval * 1; } else {
} alertinterval = alertinterval * 1;
}
lastGpsUpdateTimestamp = currentTimestamp; lastGpsUpdateTimestamp = currentTimestamp;
for (var i = 0; i < packages.length; i++) { for (var i = 0; i < packages.length; i++) {
if (packages[i].distance * 1 < getStorage("alertradius") * 1) { if (packages[i].distance * 1 < getStorage("alertradius") * 1) {
if (packages[i].lastAlert > currentTimestamp - alertinterval) { if (packages[i].lastAlert > currentTimestamp - alertinterval) {
continue; continue;
} }
if (getUndeliveredCount(packages[i]) == 0) { if (getUndeliveredCount(packages[i]) == 0) {
continue; continue;
} }
try { try {
if (map != null) { if (map != null) {
map.updatePackageLayer(packages); 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"); playSound("alert");
packages[i].lastAlert = currentTimestamp; packages[i].lastAlert = currentTimestamp;
}
} }
} }
} else {
if (map != null) {
map.startLocateControl();
mapLocationControlStarted = true;
}
} }
} else { }, function (err) {
if (map != null) { if (typeof error == "function") {
map.startLocateControl(); error(err.message);
mapLocationControlStarted = true;
} }
} }, {
}, function (err) { enableHighAccuracy: true,
if (typeof error == "function") { timeout: 5000,
error(err.message); maximumAge: 0
}
}, {
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: '<i class="fas fa-compass"></i> ' + error,
position: "bottom",
destroyOnClose: true,
closeTimeout: 1000 * 3
}); });
} 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: '<i class="fas fa-compass"></i> ' + error,
position: "bottom",
destroyOnClose: true,
closeTimeout: 1000 * 3
});
}
} }
} }

@ -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 // Make sure the status bar color is set properly
applyColorTheme(); applyColorTheme();
// start watching user position after everything's done, hopefully this will make it work
// with ios
setupPositionWatcher();
}, false); }, false);
openBrowser = function (url) { openBrowser = function (url) {
cordova.InAppBrowser.open(url, '_blank', 'location=yes'); cordova.InAppBrowser.open(url, '_blank', 'location=yes');
@ -266,6 +266,8 @@ function initNW() {
require('nw.gui').Shell.openExternal($(this).attr("href")); require('nw.gui').Shell.openExternal($(this).attr("href"));
evt.preventDefault(); evt.preventDefault();
}); });
setupPositionWatcher();
} }
function initBrowser() { function initBrowser() {
@ -285,6 +287,8 @@ function initBrowser() {
window.open($(this).attr("href"), "_blank"); window.open($(this).attr("href"), "_blank");
evt.preventDefault(); evt.preventDefault();
}); });
setupPositionWatcher();
} }
function initPlatform() { function initPlatform() {

Loading…
Cancel
Save