Open facundopereztomasek opened 6 years ago
I likewise hacked around the problem with some manual geometry corrections. Unfortunately this makes the animation a little wonky (the apparent center of the map doesn't move monotonically) but at least it zooms to the right place. Proper support for constraining geometry to a subset of the canvas would be a godsend!
onClick={() => {
let { lng, lat } = window.map.getCenter();
let zoom = window.map.getZoom() + 1;
// Correction for sidebar.
if (this.props.showingSidebar) {
if (this.props.sidebarVertical) {
lng -=
(sidebarWidthFraction / 4) *
(window.map.getBounds().getEast() -
window.map.getBounds().getWest());
} else {
lat +=
(sidebarHeightFraction / 4) *
(window.map.getBounds().getNorth() -
window.map.getBounds().getSouth());
}
}
window.map &&
window.map.easeTo({
zoom: zoom,
center: { lng, lat },
});
}}
I used different side windows on top of mapbox, like search results and item details. The mapbox container's width is 100% always, even when side windows are visible. I needed to change the zoom center, so when the user clicks zoom-in or zoom-out, the zoom center is the middle of the visible area of the background map.
Before side window showing up, the ZoomIn and ZoomOut controls should zoom with the white circle as center of zoom.
After side window showing up, the ZoomIn and ZoomOut controls should zoom with the white circle as center of zoom.
I solved it modifying the map object method setZoom:
It worked, but it's seems a bit smelly to me.
Is there another way to achieve this? If not, it could be a necessary feature for single page apps with side menus, as it happened to me.
Thanks