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

Use Geolocation API to center map by current location on load in certain cases #378

Closed
wants to merge 4 commits into from

Conversation

mwpark2014
Copy link
Contributor

@mwpark2014 mwpark2014 commented Dec 23, 2022

See #192

This is

v0: If they already have location services allowed AND if there's no pos/zoom hash in the url, use Geolocation API to center map

Unfortunately, it appears that checking if a user has already given location permissions is only possible through an async promise. Therefore, the options are either making the map load wait until we can pass in user's geo coords or recentering the map after the mapBox object has been created.
In my approach, I try to kick off the permissions query asap. Inside a useEffect hook, we recenter the map as soon as the permissions query promise resolves and there is a map object in state. This is not guaranteed to run before mapboxMap kicks off its load event.

Assumptions:

  • This app only supports modern browsers due to support for the Permissions API: https://caniuse.com/permissions-api
  • This code is only ran client-side so we don't run into any node shenanigans

Manually Tested:

  • User does not get prompted for location permissions
  • If there is a hash coordinate already present, map will not recenter
  • If user does have permissions and there is no existing hash coord, map will load up centered at user's location

@mwpark2014 mwpark2014 changed the title Use Geolocation API to center map by current location on load in certain cases #192 Use Geolocation API to center map by current location on load in certain cases Dec 23, 2022
@mwpark2014 mwpark2014 force-pushed the mwpark2014/feature/center-by-location branch from 90476e6 to 67b1976 Compare December 23, 2022 18:01
@mwpark2014 mwpark2014 linked an issue Dec 24, 2022 that may be closed by this pull request
@mwpark2014 mwpark2014 force-pushed the mwpark2014/feature/center-by-location branch from 67b1976 to c325187 Compare January 1, 2023 02:28
@mwpark2014
Copy link
Contributor Author

@mwpark2014 mwpark2014 closed this Feb 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

default center of map to user's browser data?
1 participant