diff --git a/src/ui/control/geolocate_control.js b/src/ui/control/geolocate_control.js index 71a5a593a0d..19d0988f559 100644 --- a/src/ui/control/geolocate_control.js +++ b/src/ui/control/geolocate_control.js @@ -295,7 +295,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}); +});