传单用户触发的事件 [英] Leaflet User Triggered Events

查看:131
本文介绍了传单用户触发的事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么方法可以确定事件是通过编程方式触发还是由用户触发?

Is there any way to determine whether an event was triggered programmatically or by a user?

我们想在地图移动或缩放时重新加载标记列表,但我们最初是使用setBounds()(

We want to reload marker listings when the map moves or zooms, but we are initially setting the bounds of the map with 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) events which is causing the markers to reload twice.

推荐答案

事件对象hard似乎有一个(未记录的)属性,该属性在地图被setBounds移动且未移动时设置在用户拖动地图或使用光标时设置:

There seems to be a (undocumented) property on the event object called hard that gets set when the map is moved by setBounds and doesn't get set when the user drags the map or uses the cursors:

map.on('moveend', function (e) {
    if (e.hard) {
        // moved by bounds
    } else {
       // moved by drag/keyboard
    }
});

此处位于Plunker上的测试用例: http://plnkr.co/edit/SloKuB?p=preview

Testcase here on Plunker: http://plnkr.co/edit/SloKuB?p=preview

作为另一种选择,您可以在设置边界之后将其绑定到事件,因此在设置边界时不会触发,并且当您确实要设置边界之后,可以先使用.off取消绑定并再次重新绑定用.on设置后.像(未经测试/hacky)之类的东西:

As another option you could bind to event after you've set the bounds so it won't fire when you set the bounds and when you do want to set the bounds afterwards you could first unbind using .off and rebind again after setting with .on. Something like (untested/hacky):

function moveEndHandler () {
    ....
}

map.on('moveend', moveEndHandler);

function mySetBounds (bounds) {
    map.off('moveEnd', moveEndHandler);
    map.setBounds(bounds);
    map.on('moveend', moveEndHandler);
}

这篇关于传单用户触发的事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆