leaflet-sidebar-v2
leaflet-sidebar-v2 copied to clipboard
Zoom home offset if sidebar is open
Hi guys, this is my webmap:
www.mainjoin.eu/map/santiago/
Everything works perfectly: sidebar(v2), autopan, popup content go in sidebar panels fine.
Only a few trouble. At top right I have zoom home button. When sidebar is close everything go fine: zoom home button center my map. When sidebar is open the zoom home makes the same thing but the sidebar hide part of my center map. I'd like a little right-offset on result of zoom home button when sidebar is open. Something like:
if sidebar.close() = true
zoomhome
else
zoomhome.offset
Any advices are welcome, thanks! Valerio
Hi, as you noticed the current autopan implementation does not support this usecase currently. Your approach should work well; to retrieve the offset you can try the following code:
offset = document.querySelector('.leaflet-sidebar-content').getBoundingClientRect().width
map.fitBounds(homeBounds, { paddingTopLeft: [0, offset] })
In the future i would like to support this use case natively by using Leaflet-active-area for the autopan feature (Pull Requests are welcome!)
Thank you @noerw, I'll try your code.. by chance you know of a demo example of offset on if condition? If yuo need tests on future natively I'm here :).
For those folks looking for a solution to this problem, here is the combination of @valeriodeluca and @noerw responses using Leaflet easyButton:
map_bounds = map.getBounds();
L.easyButton('fa-home', function (btn, map) {
if(sidebar.close() === true) {
offset = document.querySelector('.leaflet-sidebar-content').getBoundingClientRect().width;
map.fitBounds(map_bounds, {paddingTopLeft: [0, offset]});
}else{
map.fitBounds(map_bounds);
}
}, 'Zoom to home', {
position: 'bottomright'
}).addTo(map);
Thanks @valeriodeluca for opening this issue.