From 2693518e8d042b3120c33f08433abbc3b114d25c Mon Sep 17 00:00:00 2001 From: Arindam Bose Date: Tue, 10 Sep 2019 11:54:02 -0700 Subject: [PATCH] GeolocateControls no longer stops following when the map resizes This primarily fixes the use-case described in #7053 --- src/ui/control/geolocate_control.js | 3 +- src/ui/map.js | 4 +-- test/unit/ui/control/geolocate.test.js | 44 +++++++++++++++++++++++++- 3 files changed, 47 insertions(+), 4 deletions(-) diff --git a/src/ui/control/geolocate_control.js b/src/ui/control/geolocate_control.js index 69decb327c0..e6df08589c2 100644 --- a/src/ui/control/geolocate_control.js +++ b/src/ui/control/geolocate_control.js @@ -296,7 +296,8 @@ class GeolocateControl extends Evented { // the watch mode to background watch, so that the marker is updated but not the camera. if (this.options.trackUserLocation) { this._map.on('movestart', (event) => { - if (!event.geolocateSource && this._watchState === 'ACTIVE_LOCK') { + const fromResize = event.originalEvent && event.originalEvent.type === 'resize'; + if (!event.geolocateSource && this._watchState === 'ACTIVE_LOCK' && !fromResize) { this._watchState = 'BACKGROUND'; this._geolocateButton.classList.add('mapboxgl-ctrl-geolocate-background'); this._geolocateButton.classList.remove('mapboxgl-ctrl-geolocate-active'); diff --git a/src/ui/map.js b/src/ui/map.js index 5b9286cda4d..9c240888a5b 100755 --- a/src/ui/map.js +++ b/src/ui/map.js @@ -1849,9 +1849,9 @@ class Map extends Camera { this._update(); } - _onWindowResize() { + _onWindowResize(event: Event) { if (this._trackResize) { - this.resize()._update(); + this.resize({originalEvent: event})._update(); } } diff --git a/test/unit/ui/control/geolocate.test.js b/test/unit/ui/control/geolocate.test.js index c0c63906749..beda845f55e 100644 --- a/test/unit/ui/control/geolocate.test.js +++ b/test/unit/ui/control/geolocate.test.js @@ -157,7 +157,7 @@ test('GeolocateControl no watching map camera on geolocation', (t) => { const click = new window.Event('click'); - map.on('moveend', () => { + map.once('moveend', () => { t.deepEqual(lngLatAsFixed(map.getCenter(), 4), {lat: 10, lng: 20}, 'map centered on location'); const mapBounds = map.getBounds(); @@ -334,3 +334,45 @@ test('GeolocateControl trackuserlocationstart event', (t) => { geolocate._geolocateButton.dispatchEvent(click); geolocation.send({latitude: 10, longitude: 20, accuracy: 30, timestamp: 40}); }); + +test('GeolocateControl does not switch to BACKGROUND and stays in ACTIVE_LOCK state on window resize', (t) => { + const map = createMap(t); + const geolocate = new GeolocateControl({ + trackUserLocation: true, + }); + map.addControl(geolocate); + + const click = new window.Event('click'); + + geolocate.once('geolocate', () => { + t.equal(geolocate._watchState, 'ACTIVE_LOCK'); + window.dispatchEvent(new window.Event('resize')); + t.equal(geolocate._watchState, 'ACTIVE_LOCK'); + t.end(); + }); + + geolocate._geolocateButton.dispatchEvent(click); + geolocation.send({latitude: 10, longitude: 20, accuracy: 30, timestamp: 40}); +}); + +test('GeolocateControl switches to BACKGROUND state on map manipulation', (t) => { + const map = createMap(t); + const geolocate = new GeolocateControl({ + trackUserLocation: true, + }); + map.addControl(geolocate); + + const click = new window.Event('click'); + + geolocate.once('geolocate', () => { + t.equal(geolocate._watchState, 'ACTIVE_LOCK'); + map.jumpTo({ + center: [0, 0] + }); + t.equal(geolocate._watchState, 'BACKGROUND'); + t.end(); + }); + + geolocate._geolocateButton.dispatchEvent(click); + geolocation.send({latitude: 10, longitude: 20, accuracy: 30, timestamp: 40}); +});