加载事件不在传单中触发

时间:2015-06-25 06:24:46

标签: javascript leaflet

我试图在用户完成某些事件时在地图上执行业务逻辑。

  

我能够拖动 dblclick zoomstart 事件。

但是加载事件并没有被我解雇。 (最初在浏览器加载时)

我的示例代码如下:

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

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

3 个答案:

答案 0 :(得分:5)

这可以在调用setView时完成,这会使地图触发加载事件。

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/

答案 1 :(得分:0)

here起,您可以使用该事件

  

'空闲'

而不是

  

'负载'

答案 2 :(得分:0)

如果您只想在地图准备好时执行某些操作,请使用 map.whenReady(fn)

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

更长的解释: 我遇到了同样的问题(使用 LeafletJS 1.6 和 1.7.1),除非我在设置 setView 之前定义它,否则不会触发“空闲”和“加载”。

挖掘源代码,我发现还有 map.whenReady(function(){}); 对我有用,如果地图已经加载也可以使用。

所以代替:

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

使用:

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