Skip to content

Commit

Permalink
GeolocateControls no longer stops following when the map resizes
Browse files Browse the repository at this point in the history
This primarily fixes the use-case described in #7053
  • Loading branch information
Arindam Bose authored Sep 10, 2019
1 parent a0d33a1 commit 2693518
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 4 deletions.
3 changes: 2 additions & 1 deletion src/ui/control/geolocate_control.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
4 changes: 2 additions & 2 deletions src/ui/map.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}

Expand Down
44 changes: 43 additions & 1 deletion test/unit/ui/control/geolocate.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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});
});

0 comments on commit 2693518

Please sign in to comment.