Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

window resize keeps map geolocate control following #8736

Merged
merged 3 commits into from
Sep 10, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/ui/control/geolocate_control.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
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});
});