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

Allow GeolocateControl to be cancellable #6789

Open
aendra-rininsland opened this issue Jun 8, 2018 · 3 comments
Open

Allow GeolocateControl to be cancellable #6789

aendra-rininsland opened this issue Jun 8, 2018 · 3 comments

Comments

@aendra-rininsland
Copy link
Contributor

aendra-rininsland commented Jun 8, 2018

Motivation

My particular use case involves geolocating UK users and firing the normal GeolocateControl behaviour if they're within the UK, displaying error messaging if they're outside it. Currently the geolocate event isn't cancellable because it merely contains the Position data and not any methods governing how the rest of the interaction works.

I've added a Stack Overflow question here in case there's a workaround I'm missing but having looked through the source of geolocate_control.js it really doesn't seem possible at present.

Design

geolocate should be a cancelable event; calling event.preventDefault should cause the behaviour of zooming/panning to the user position and placing the relevant marker to be cancelled. This can be accomplished by moving the firing of the geolocate event from here to the beginning of the _onSuccess callback.

@andrewharvey
Copy link
Collaborator

👍 from me for making the GeolocateControl support this use case. Did you want to put together a PR which makes it possible?

I guess the workaround at the moment would be to replace the private _updateCamera in your application from

_updateCamera(position: Position) {
const center = new LngLat(position.coords.longitude, position.coords.latitude);
const radius = position.coords.accuracy;
this._map.fitBounds(center.toBounds(radius), this.options.fitBoundsOptions, {
geolocateSource: true // tag this camera change so it won't cause the control to change to background state
});
}

to something which first checks if the position is within some polygon/bounds you define and from there either fit to bounds or display your error message.

@aendra-rininsland
Copy link
Contributor Author

Hi!

Thanks for the workaround, that was super helpful, I've been able to resolve my issue as a result.

If you're still wanting a PR for cancellable geolocate events let me know and I'll try to submit one after I finish this project. 👍

@aptlin
Copy link

aptlin commented Mar 22, 2019

@Aendrew, any updates on this? Would be very grateful if you implement this feature, for example, by forcing the control to respect the maxBounds of the map instance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants