Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

leaflet.js: Fire event when setView() has finished the animation. How is that possible?

I'm currently working with leaflet.js and I'm searching the web now for quite a bit, to find out: If there is an event, that can be fired after animation end of asynchronous function setView().

This is what I tried:

map.setView( [lat, lon ], 12 , { 
   pan: { animate: true , duration: 0.5 }, 
   zoom: { animate: true }, 
   animate: true
}.on('ready', function(e) {
   console.log("animation finished!");
});

The setView-command works perfectly, but the function, that should be fired when map-animation is ready, is not working.

Does anyone have a solution?

like image 674
codiga Avatar asked Nov 06 '15 10:11

codiga


People also ask

What is setView in Leaflet?

setView : Set the view of the map (center and zoom level) flyTo : Flys to a given location/zoom-level using smooth pan-zoom. fitBounds : Set the bounds of a map. flyToBounds : Flys to given bound using smooth pan/zoom. setMaxBounds : Restricts the map view to the given bounds.

How do you remove attribution in Leaflet?

Yes, you are free to remove the Leaflet attribution. You can remove the entire attribution control when you create the map by setting attributionControl: false. If you just want to remove (or change) the default Leaflet attribution, you can do something like this: map. attributionControl.

What is TileLayer in Leaflet?

Used to load and display tile layers on the map, implements ILayer interface.


1 Answers

Have you tried the moveend and zoomend events?

You can attach a callback on both events easily: map.once("moveend zoomend", callback) (note the once that will remove the listener once it is triggered)

A very special case may not trigger your callback: if setView does not change anything, i.e. the requested view is already the current view. To be checked, it is possible that the moveend event is fired by setView in all cases.

Otherwise, a simple timeout could be enough.Especially since you specify the animation duration.

like image 125
ghybs Avatar answered Sep 19 '22 12:09

ghybs