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

When zooming in or out on the map it doesn't keep map center in the center of the map. #515

Closed
mglind opened this issue May 16, 2018 · 4 comments
Assignees
Labels

Comments

@mglind
Copy link

mglind commented May 16, 2018

The API allows passing scrollZoom as an object. This object allows the map to zoom around center.

https://www.mapbox.com/mapbox-gl-js/api#scrollzoomhandler#enable

This wrapper allows a passing a boolean to scrollZoom.

It would be nice to both enable zooming and allow it to zoom around the map center.

@goldpbear
Copy link

goldpbear commented Feb 20, 2019

@mglind I agree this would be a nice add.

If you're looking for a workaround to simulate the "zoom around center" behavior in the meantime, I recently had some success with this approach: simply don't update the latitude and longitude props in state if the zoom prop also changed in the same update.

One gotcha with this approach is if your map transitions using a transition method that changes zoom as well as position (like the FlyTo transition), then you'll need an extra check to allow simultaneous latitude/longitude and zoom updating when the map is transitioning. Otherwise your flyTo transition will sort of short-circuit. To do this I maintain an extra piece of state, isMapTransitioning, which is set and unset using the onTransitionStart and onTransitionEnd callbacks.

Hope that makes sense...

@Khalidalansi
Copy link

@goldpbear please can you add example ?

@Pessimistress
Copy link
Collaborator

This is being addressed in v7.0. Please follow #1646

@NameFILIP
Copy link
Contributor

@goldpbear thanks for the suggestion. I've implemented it as follows:

onViewStateChange={({ viewState, oldViewState, interactionState }) => {
  if (interactionState.isZooming) {
    setViewState({
      ...viewState,
      // Keep the old lat/lng to zoom around the center of the map
      longitude: oldViewState.longitude,
      latitude: oldViewState.latitude,
    });
  } else {
    setViewState(viewState);
  }
}}

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

No branches or pull requests

5 participants