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;
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: '<i class="fas fa-compass"></i> ' + 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: '<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
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() {

Loading…
Cancel
Save