admin管理员组

文章数量:1392081

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.
  });

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.
  });
Share Improve this question edited Jun 25, 2015 at 6:38 ram 2,3434 gold badges29 silver badges39 bronze badges asked Jun 25, 2015 at 6:24 Arun KumarArun Kumar 1292 silver badges13 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 5

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./Leaflet/Leaflet/issues/3560

http://jsfiddle/QUGyr/1/

Use map.whenReady(fn) if you just want to do something when the map is ready:

map.whenReady(function(){
    console.log('Map Loaded!');
});

Longer explanation: I'm having the same issue (with LeafletJS 1.6 and 1.7.1) with 'idle' and 'load' not triggering unless I define it before setting setView.

Digging through the source I discovered there's also map.whenReady(function(){}); which is working for me and also works if the map is already loaded.

So instead of:

map.on('load', function(){
    console.log('Map Loaded!');
});

Use:

map.whenReady(function(){
    console.log('Map Loaded!');
});

As from here you can use the event

'idle'

instead of

'load'

本文标签: javascriptload event not firing in leafletStack Overflow