传单用户触发事件
Leaflet User Triggered Events
有什么方法可以确定事件是通过编程方式还是由用户触发的?
我们想在地图移动或缩放时重新加载标记列表,但我们最初使用 setBounds()
(http://leafletjs.com/reference.html#rectangle-setbounds) which is also triggering the moveend
(http://leafletjs.com/reference.html#map-moveend) and 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
作为另一种选择,您可以在设置边界后绑定到事件,这样它就不会在您设置边界时触发,并且当您之后确实想设置边界时,您可以先使用 [=14= 解除绑定] 设置后重新绑定.on
。像 (untested/hacky):
function moveEndHandler () {
....
}
map.on('moveend', moveEndHandler);
function mySetBounds (bounds) {
map.off('moveEnd', moveEndHandler);
map.setBounds(bounds);
map.on('moveend', moveEndHandler);
}
有什么方法可以确定事件是通过编程方式还是由用户触发的?
我们想在地图移动或缩放时重新加载标记列表,但我们最初使用 setBounds()
(http://leafletjs.com/reference.html#rectangle-setbounds) which is also triggering the moveend
(http://leafletjs.com/reference.html#map-moveend) and 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
作为另一种选择,您可以在设置边界后绑定到事件,这样它就不会在您设置边界时触发,并且当您之后确实想设置边界时,您可以先使用 [=14= 解除绑定] 设置后重新绑定.on
。像 (untested/hacky):
function moveEndHandler () {
....
}
map.on('moveend', moveEndHandler);
function mySetBounds (bounds) {
map.off('moveEnd', moveEndHandler);
map.setBounds(bounds);
map.on('moveend', moveEndHandler);
}