Mapbox-gl-js: Allow GeolocateControl to be cancellable

Created on 8 Jun 2018  路  3Comments  路  Source: mapbox/mapbox-gl-js

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.

feature

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 _updateCamera in your application from

https://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.

All 3 comments

:+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

https://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.

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.

Was this page helpful?
0 / 5 - 0 ratings