From d1a6ab04be0e067f7aaf90ac8f255bcab37bc799 Mon Sep 17 00:00:00 2001 From: ryanhamley Date: Tue, 10 Dec 2019 17:32:24 -0800 Subject: [PATCH 1/3] Force immediate geolocation when multiple watches occur --- debug/multiple.html | 11 +++++++++++ src/ui/control/geolocate_control.js | 24 +++++++++++++++++++++++- 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/debug/multiple.html b/debug/multiple.html index 342bc774a9f..6c279c00b7c 100644 --- a/debug/multiple.html +++ b/debug/multiple.html @@ -63,6 +63,17 @@ style, hash: false }); + + map.addControl(new mapboxgl.GeolocateControl({ + positionOptions: { + enableHighAccuracy: true + }, + trackUserLocation: true, + showUserLocation: true, + fitBoundsOptions: { + maxZoom: 20 + } + })); } var containers = document.querySelectorAll('.map'); diff --git a/src/ui/control/geolocate_control.js b/src/ui/control/geolocate_control.js index 0f9a0e6a894..833e5e44009 100644 --- a/src/ui/control/geolocate_control.js +++ b/src/ui/control/geolocate_control.js @@ -53,6 +53,9 @@ function checkGeolocationSupport(callback) { } } +let numberOfWatches = 0; +let noTimeout = false; + /** * A `GeolocateControl` control provides a button that uses the browser's geolocation * API to locate the user on the map. @@ -134,6 +137,8 @@ class GeolocateControl extends Evented { DOM.remove(this._container); this._map = (undefined: any); + numberOfWatches = 0; + noTimeout = false; } _isOutOfMapMaxBounds(position: Position) { @@ -270,6 +275,11 @@ class GeolocateControl extends Evented { if (this._geolocationWatchID !== undefined) { this._clearWatch(); } + } else if (error.code === 3 && noTimeout) { + // this represents a forced error state + // this was triggered to force immediate geolocation when a watch is already present + // see https://github.com/mapbox/mapbox-gl-js/issues/8214 + return; } else { this._setErrorState(); } @@ -366,6 +376,8 @@ class GeolocateControl extends Evented { case 'ACTIVE_ERROR': case 'BACKGROUND_ERROR': // turn off the Geolocate Control + numberOfWatches--; + noTimeout = false; this._watchState = 'OFF'; this._geolocateButton.classList.remove('mapboxgl-ctrl-geolocate-waiting'); this._geolocateButton.classList.remove('mapboxgl-ctrl-geolocate-active'); @@ -423,8 +435,18 @@ class GeolocateControl extends Evented { this._geolocateButton.classList.add('mapboxgl-ctrl-geolocate-waiting'); this._geolocateButton.setAttribute('aria-pressed', 'true'); + numberOfWatches++; + let options; + if (numberOfWatches > 1) { + options = {maximumAge:600000, timeout:0}; + noTimeout = true; + } else { + options = this.options.positionOptions; + noTimeout = false; + } + this._geolocationWatchID = window.navigator.geolocation.watchPosition( - this._onSuccess, this._onError, this.options.positionOptions); + this._onSuccess, this._onError, options); } } else { window.navigator.geolocation.getCurrentPosition( From 185b9b6bde8f409cbf018b9a85bb8a2e89c27d60 Mon Sep 17 00:00:00 2001 From: ryanhamley Date: Tue, 10 Dec 2019 17:38:26 -0800 Subject: [PATCH 2/3] Add to comment --- src/ui/control/geolocate_control.js | 1 + 1 file changed, 1 insertion(+) diff --git a/src/ui/control/geolocate_control.js b/src/ui/control/geolocate_control.js index 833e5e44009..071a7140d36 100644 --- a/src/ui/control/geolocate_control.js +++ b/src/ui/control/geolocate_control.js @@ -279,6 +279,7 @@ class GeolocateControl extends Evented { // this represents a forced error state // this was triggered to force immediate geolocation when a watch is already present // see https://github.com/mapbox/mapbox-gl-js/issues/8214 + // and https://w3c.github.io/geolocation-api/#example-5-forcing-the-user-agent-to-return-a-fresh-cached-position return; } else { this._setErrorState(); From e4c76b9d2ec9f714675ecd81216278cb2e6d0037 Mon Sep 17 00:00:00 2001 From: ryanhamley Date: Thu, 2 Jan 2020 16:54:52 -0800 Subject: [PATCH 3/3] Rename options to positionOptions --- src/ui/control/geolocate_control.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/ui/control/geolocate_control.js b/src/ui/control/geolocate_control.js index 071a7140d36..d9b024753c2 100644 --- a/src/ui/control/geolocate_control.js +++ b/src/ui/control/geolocate_control.js @@ -437,17 +437,17 @@ class GeolocateControl extends Evented { this._geolocateButton.setAttribute('aria-pressed', 'true'); numberOfWatches++; - let options; + let positionOptions; if (numberOfWatches > 1) { - options = {maximumAge:600000, timeout:0}; + positionOptions = {maximumAge:600000, timeout:0}; noTimeout = true; } else { - options = this.options.positionOptions; + positionOptions = this.options.positionOptions; noTimeout = false; } this._geolocationWatchID = window.navigator.geolocation.watchPosition( - this._onSuccess, this._onError, options); + this._onSuccess, this._onError, positionOptions); } } else { window.navigator.geolocation.getCurrentPosition(