From f9a7c1c86d87aebae2fba9993cee3dedbceb89e0 Mon Sep 17 00:00:00 2001 From: Matthias Wirth Date: Thu, 3 Jun 2021 07:02:48 +0200 Subject: [PATCH] watchPosition geolocation doesn't seem to work in some browsers, also poll every 30 sec --- html/script.js | 43 +++++++++++++++++++++++++++---------------- 1 file changed, 27 insertions(+), 16 deletions(-) diff --git a/html/script.js b/html/script.js index 5fb89e4..7875a47 100644 --- a/html/script.js +++ b/html/script.js @@ -3461,6 +3461,9 @@ function resetMap() { //selectPlaneByHex(null,false); jQuery("#update_error").css('display','none'); + + // enable position watch on this user interaction, maybe improve reliability + watchPosition(); } function updateMapSize() { @@ -4301,7 +4304,7 @@ function changeCenter(init) { const rawCenter = OLMap.getView().getCenter(); const center = ol.proj.toLonLat(rawCenter); - const centerChanged = (CenterLon != center[0] || CenterLat != center[1]); + const centerChanged = (Math.abs(center[1] - CenterLat) < 0.000001 && Math.abs(center[0] - CenterLon) < 0.000001); localStorage['CenterLon'] = CenterLon = center[0]; localStorage['CenterLat'] = CenterLat = center[1]; @@ -5203,7 +5206,8 @@ function setLineWidth() { bgFill = new ol.style.Stroke({color: 'rgba(0,0,0,0.25'}); } function onLocationChange(position) { - const moveMap = (Math.abs(SiteLat - CenterLat) < 0.00001 && Math.abs(SiteLon - CenterLon) < 0.00001); + changeCenter(); + const moveMap = (Math.abs(SiteLat - CenterLat) < 0.000001 && Math.abs(SiteLon - CenterLon) < 0.000001); SiteLat = CenterLat = DefaultCenterLat = position.coords.latitude; SiteLon = CenterLon = DefaultCenterLon = position.coords.longitude; @@ -5221,6 +5225,18 @@ function logArg(error) { console.log(error); } +let watchPositionId; +function watchPosition() { + const geoposOptions = { + enableHighAccuracy: false, + timeout: Infinity, + maximumAge: 5 * 1000, + }; + if (watchPositionId != null) + navigator.geolocation.clearWatch(watchPositionId); + watchPositionId = navigator.geolocation.watchPosition(onLocationChange, logArg, geoposOptions); +} + function geoFindMe() { if (SiteOverride || (!globeIndex && !uuid && !askLocation)) { initSitePos(); @@ -5254,30 +5270,25 @@ function geoFindMe() { const geoposOptions = { enableHighAccuracy: false, timeout: Infinity, - maximumAge: 300, + maximumAge: 300 * 1000, }; navigator.geolocation.getCurrentPosition(success, error, geoposOptions); - if (1) { - const geoposOptions = { - enableHighAccuracy: false, - timeout: Infinity, - maximumAge: 60, - }; - navigator.geolocation.watchPosition(onLocationChange, logArg, geoposOptions); - } + watchPosition(); - if (0) { + // interval position polling every half minute for browsers that are shit + if (1) { + const pollSeconds = 30; window.setInterval(function() { if (tabHidden) return; const geoposOptions = { - enableHighAccuracy: true, - timeout: 15, - maximumAge: 60, + enableHighAccuracy: false, + timeout: pollSeconds * 1000, + maximumAge: 180 * 1000 , }; navigator.geolocation.getCurrentPosition(onLocationChange, logArg, geoposOptions); - }, 60000); + }, pollSeconds * 1000); } } }