我想以编程方式触发select事件,但我不知道如何。所以,这就是我创建选择互动的方式:
var selection = new ol.interaction.Select({
...
});
selection.on('select', function (event) {
... here is some action or event I want to trigger
});
在我的代码的其他部分,我将新功能推送到选择中:
selection.getFeatures().push(new_feature);
我想要的是触发select
事件。我怎么能这样做?
答案 0 :(得分:2)
你不应该手动触发该事件。应该由交互本身来触发它。我会做的是听取不同类型的事件。
ol.interaction.Select
有一个功能集合,即ol.Collection
。当交互选择功能时,该对象会触发add
和remove
事件,即通过其自己的处理程序和的结果,手动推送/删除的结果。
这是一个片段:
var featuresCollection = selection.getFeatures();
featuresCollection.on('add', function() {
// do what you want on add
});
featuresCollection.on('remove', function() {
// do what you want on remove
});
这需要更多的管理,因为回调方法是每个功能 ,但最终你可以做你想做的事。