javascriptleaflet

load event not firing in leaflet


I'm trying to do a business logic on a map whenever some events are done by a user.

I'm able to get working drag , dblclick and zoomstart events.

But load event is not getting fired for me. (On Browser load initially)

My sample code below :

  var map = L.map('map').setView([34.7320,-86.5966], 14);

  map.on('load drag dblclick zoomstart', function() {      
         // My business logic goes here.
  });

Solution

  • Make sure .on occurs before .setView

    This can be done when you call setView, that makes the map fire the load event.

    var map = L.map('map').on('load', function(){
      // Your business logic here...
    }).setView([34.7320,-86.5966], 14);
    

    (OR)

    https://github.com/Leaflet/Leaflet/issues/3560

    http://jsfiddle.net/QUGyr/1/