-
Notifications
You must be signed in to change notification settings - Fork 2.2k
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's geolocation to be triggered programmatically #5464
Comments
👍 I prefer the second option. Did you want to put this together in a PR? |
This is not possible? It's a deal breaker. Found this simple hack:
|
simple hack
|
@amirping I think that GeolocateControl can now be triggered thanks to @andrewharvey https://www.mapbox.com/mapbox-gl-js/api/#geolocatecontrol#trigger Using example in original post:
Set up load map event listener
Set up event listener for when a new location has been obtained
I'm still a beginner at this, so any helpful comments welcomed. |
@BloodSugar327 Thank you so much, I was trying to understing how I could do this! |
thanks, |
Motivation
The Geolocate control allows the user's location to be obtained when the user clicks on the associated UI button. However, there is currently no way to have the geolocate functionality trigger when the map first loads.
At present, the map loads and displays the default location it is given. It would be useful to trigger the geolocate functionality on load so the map automatically centers on the user's location without the user first having to click the Geolocate control button.
Design Alternatives
This is my current solution:
The logic performed by
addControl
is not entirely synchronous, therefore, it is necessary to delay calling_onClickGeolocate
until the relevant DOM elements exist.This solution is not ideal as it requires calling a private method and introduces a race condition mediated by an arbitrary time delay.
Design
Two proposals:
GeolocateControl
allowing the programmer to trigger the geolocation functionality, along with aready
event that is fired once theGeoLocate
control has been added to the map and all DOM elements have been created.The latter proposal offers more flexibility to the programmer, while the former is more restrictive and only affords the programmer the additional ability to have the geolocate functionality trigger when the control is first added.
I am in favor of the second proposal as it enables the programmer to trigger geolocation at any time after the control has been added to the map as opposed to just initially.
The text was updated successfully, but these errors were encountered: