leaflet-sidebar-v2 icon indicating copy to clipboard operation
leaflet-sidebar-v2 copied to clipboard

Zoom home offset if sidebar is open

Open valeriodeluca opened this issue 6 years ago • 3 comments

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

valeriodeluca avatar May 22 '18 17:05 valeriodeluca

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!)

noerw avatar May 22 '18 22:05 noerw

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 :).

valeriodeluca avatar May 23 '18 07:05 valeriodeluca

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.

jcla490 avatar Jun 28 '19 01:06 jcla490