Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Within leaflet, does fitBounds have a callback?

I have two types of searches for putting markers on a map. One search fires based on a search query from an input form and the other fires on the moveend event (i.e. "redo search on map movement"). The resultant data is not the same; the former uses a radius and the latter limits to within the bounds).

The issue is that with the query based search, I would like to fit the map to the markers returned. So, I use fitBounds() for this which delegates internally and animates a pan to the best bounds around the markers.

At the end of the animation it fires a moveend event firing off my other type of search and resetting my results, which is the behavior I do not want.

In order to work around the problem, I can use setTimeout and a known duration for the panning options on fitBounds(). With this, I can turn my event listener on moveend off, then fitBounds which takes .25 seconds, then setTimeout for .25 seconds (in practice, a little longer) with a callback to add the moveend listener back.

This works but it would be more practical if fitBounds or panning had an event or callback itself. Can you think of a better solution?

like image 284
maletor Avatar asked Jan 28 '16 00:01

maletor


1 Answers

You can use moveend + zoomend events to catch end of map fitting.
Because at least one of them called in the end of fitBounds execution

like image 77
gdbdable Avatar answered Sep 28 '22 08:09

gdbdable