是否有任何方法确定事件是以编程方式触发的,还是由用户触发的?
我们希望在地图移动或缩放时重新加载标记列表,但是我们最初使用setBounds() (http://leafletjs.com/reference.html#rectangle-setbounds)设置映射的边界,这也会触发moveend (http://leafletjs.com/reference.html#map-moveend)和zoomend (http://leafletjs.com/reference.html#map-zoomend)事件,这将导致标记重新加载两次。
发布于 2015-02-19 19:51:24
事件对象上似乎有一个名为hard的(未记录在案的)属性,它在setBounds移动映射时设置,在用户拖动映射或使用游标时不设置:
map.on('moveend', function (e) {
if (e.hard) {
// moved by bounds
} else {
// moved by drag/keyboard
}
});柱塞上的测试用例:http://plnkr.co/edit/SloKuB?p=preview
作为另一个选项,您可以在设置边界之后绑定到事件,以便在设置边界时不会触发,而在设置边界之后,您可以首先使用.off解除绑定,然后使用.on重新绑定。类似于(未经测试的/无趣的):
function moveEndHandler () {
....
}
map.on('moveend', moveEndHandler);
function mySetBounds (bounds) {
map.off('moveEnd', moveEndHandler);
map.setBounds(bounds);
map.on('moveend', moveEndHandler);
}https://stackoverflow.com/questions/28614582
复制相似问题