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

Add ability to fix map corner position during map.resize() #4269

Open
lucaswoj opened this issue Feb 14, 2017 · 9 comments
Open

Add ability to fix map corner position during map.resize() #4269

lucaswoj opened this issue Feb 14, 2017 · 9 comments

Comments

@lucaswoj
Copy link
Contributor

No description provided.

@allthesignals
Copy link
Contributor

Hmm any way to disable this? Or maybe I'm not understanding... after a map.resize(), the map adjusts, but its internal center also updates, meaning all it layer content is shifted over (if it's resizing to fit a now-larger container). This is a bit jarring for users...

@rychoo2
Copy link

rychoo2 commented Nov 28, 2017

Hello,
I am resizing map programatically and I have the same problem.
For time being I am using following wrapper function too keep top left map corner at the same position:

function resizeMapAndKeepNw(map) {
        let nw = map.getBounds().getNorthWest();
        map.resize();
        let newNw = map.getBounds().getNorthWest();
        let currentCenter = map.getCenter();
        let shiftVector = { x: newNw.lng - nw.lng, y: newNw.lat - nw.lat };
        let newCenter = new mapboxgl.LngLat(currentCenter.lng - shiftVector.x, currentCenter.lat - shiftVector.y);
        map.setCenter(newCenter);
}

see working example (toggle sidebar on/off):
https://codepen.io/anon/pen/NwOWJB

This solution only works well for horizontal resizing, not vertical.

@jfirebaugh jfirebaugh changed the title Preserve the camera "center" when the map is resized Add ability to fix map corner position during map.resize() Apr 13, 2018
@jfirebaugh
Copy link
Contributor

I retitled this because the old title actually described the current behavior, and I think the intent was to request the alternative behavior.

@songololo
Copy link

This would be a very useful feature for dynamic resizing events.

@dagjomar
Copy link
Contributor

Oh, yes this is actually very much wanted :)

@songololo
Copy link

songololo commented May 13, 2018

Map pitch is also not visually consistent across resize events, so would be good if both location and pitch remain visually consistent so that the map doesn't jump when resizing the window div programmatically.

@andrewharvey
Copy link
Collaborator

Does anyone else expect the map bounds to stay similar on resize, thereby changing the zoom? As a user, I think this makes more sense as it keeps the view roughly the same across resize.

@jkarttunen
Copy link

Depends on breakpoint, I'm trying to reduce motion on resizes, by attaching top left corner. Now resize seems to be centered.

@herrethan
Copy link

herrethan commented May 27, 2022

For anyone else running into this issue, I've created a codesandbox that shows how you can anchor a map to one corner when it expands (plus some timing delay to make the panel always appear on top of the map), without any visually jarring adjustments. The trick is to:

  • make sure .mapboxgl-canvas is anchored to the desired corner (eg top: 0, right: 0)
  • calculate your new center and set it within a resize handler (ie not after .resize())

This could possibly be made more generic with some more math by adjusting center based on the new container's ∆x ∆y. But you'll always need to anchor the canvas with css to a desired corner to avoid unwanted movement.

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

No branches or pull requests

9 participants