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.
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.
:+1: 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
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.
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. :+1:
@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.
Most helpful comment
:+1: 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
_updateCamerain your application fromhttps://github.com/mapbox/mapbox-gl-js/blob/a3aaa76f5ce25b027b7f2c30472d646d8a7f7392/src/ui/control/geolocate_control.js#L187-L194
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.