有什么方法可以确定事件是通过编程方式触发还是由用户触发?
我们希望在地图移动或缩放时重新加载标记列表,但我们最初使用以下命令设置地图的边界setBounds()
(http://leafletjs.com/reference.html#rectangle-setbounds http://leafletjs.com/reference.html#rectangle-setbounds)这也触发了moveend
(http://leafletjs.com/reference.html#map-moveend http://leafletjs.com/reference.html#map-moveend) and zoomend
(http://leafletjs.com/reference.html#map-zoomend http://leafletjs.com/reference.html#map-zoomend)导致标记重新加载两次的事件。
事件对象上似乎有一个(未记录的)属性,称为hard
当地图移动时设置setBounds
当用户拖动地图或使用光标时不会设置:
map.on('moveend', function (e) {
if (e.hard) {
// moved by bounds
} else {
// moved by drag/keyboard
}
});
Plunker 上的测试用例:http://plnkr.co/edit/SloKuB?p=preview http://plnkr.co/edit/SloKuB?p=preview
作为另一个选项,您可以在设置边界后绑定到事件,这样当您设置边界时它就不会触发,并且当您确实想在之后设置边界时,您可以首先使用取消绑定.off
设置后再次重新绑定.on
。像(未经测试/hacky)的东西:
function moveEndHandler () {
....
}
map.on('moveend', moveEndHandler);
function mySetBounds (bounds) {
map.off('moveEnd', moveEndHandler);
map.setBounds(bounds);
map.on('moveend', moveEndHandler);
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)