Leaflet: map.fitBounds with callback after operation end

Created on 28 Nov 2018  路  3Comments  路  Source: Leaflet/Leaflet

Would be nice to add a callback/then whenever map.fitBounds() ended its operation. Today we have to rely adding map.once('zoomend moveend', function()); to make something happen after.

For example, to make a marker bounce after appearing, i have to:

var marker = getMarker();
map.once('zoomend moveend', ()=>{bounceMarker(marker);});
map.fitBounds(marker.getBounds());

Would be nice to do something like:
map.fitBounds(marker.getBounds()).then(()=>{bounceMarker(marker);});

Most helpful comment

It would be great if map functions that modifying map state, will return Promise

All 3 comments

It would be great if map functions that modifying map state, will return Promise

What i did was, adding method to my map (uses jquery promise, but can be easily replaced):

zoomToBounds: function () {
  var promise = $.Deferred();
  this.once("moveend", function () {setTimeout(function () {promise.resolve();}, 20);});
  this.fitBounds.call(this, arguments);
  return promise;
}

Can you replace $.Deferred() with new Promise() And then to create a pull request?

Was this page helpful?
0 / 5 - 0 ratings