Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

leaflet.js set duration of fitBounds animation

I use Leaflet to show a map to the user. The map has several markers on it and when the user clicks on one, I want to pan to the marker location and zoom in. I can achieve this by using the following code:

    map.fitBounds([
        // bounds of clicked marker
    ]);

This works fine and Leaflet even animates the transition. What I would like is set the duration of the animation in miliseconds. FitBounds takes some options, but none of them allows me to specify the duration of the panning and zooming animation. Can anyone help me with this?

like image 321
WeSt Avatar asked Jan 10 '23 09:01

WeSt


2 Answers

That's incorrect, the fitBounds method does accept the zoom/pan options of which pan options has duration as stated in the documentation for the fitBounds options.

like image 166
iH8 Avatar answered Jan 27 '23 14:01

iH8


What you are looking for is flyToBounds(<LatLngBounds> bounds, <fitBounds options> options?)

Sets the view of the map with a smooth animation like flyTo, but takes a bounds parameter like fitBounds.

map.flyToBounds(bounds, { duration: 1 } );
like image 45
leonheess Avatar answered Jan 27 '23 15:01

leonheess