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

Immediately update map when more than one geolocation watch event is triggered #9092

Merged
merged 3 commits into from
Jan 6, 2020
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
11 changes: 11 additions & 0 deletions debug/multiple.html
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
25 changes: 24 additions & 1 deletion src/ui/control/geolocate_control.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ function checkGeolocationSupport(callback) {
}
}

let numberOfWatches = 0;
let noTimeout = false;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What confused me when reviewing is I thought these would be scoped for each control, but it seems they are shared globally across the page (which is why they work). I think it's worth adding a comment to explain what these are for and what they represent, and mention something about tracking number of watches on the page, even when multiple maps and geolocate controls may be present.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah they have to be scoped to the global control state so that we can track when multiple watches are being created. i will update with a comment


/**
* A `GeolocateControl` control provides a button that uses the browser's geolocation
* API to locate the user on the map.
Expand Down Expand Up @@ -134,6 +137,8 @@ class GeolocateControl extends Evented {

DOM.remove(this._container);
this._map = (undefined: any);
numberOfWatches = 0;
noTimeout = false;
}

_isOutOfMapMaxBounds(position: Position) {
Expand Down Expand Up @@ -270,6 +275,12 @@ 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
// and https://w3c.github.io/geolocation-api/#example-5-forcing-the-user-agent-to-return-a-fresh-cached-position
return;
} else {
this._setErrorState();
}
Expand Down Expand Up @@ -366,6 +377,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');
Expand Down Expand Up @@ -423,8 +436,18 @@ class GeolocateControl extends Evented {
this._geolocateButton.classList.add('mapboxgl-ctrl-geolocate-waiting');
this._geolocateButton.setAttribute('aria-pressed', 'true');

numberOfWatches++;
let positionOptions;
if (numberOfWatches > 1) {
positionOptions = {maximumAge:600000, timeout:0};
noTimeout = true;
} else {
positionOptions = this.options.positionOptions;
noTimeout = false;
}

this._geolocationWatchID = window.navigator.geolocation.watchPosition(
this._onSuccess, this._onError, this.options.positionOptions);
this._onSuccess, this._onError, positionOptions);
}
} else {
window.navigator.geolocation.getCurrentPosition(
Expand Down