如何以编程方式触发选择事件

时间:2017-06-29 09:12:00

标签: javascript openlayers-3

我想以编程方式触发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事件。我怎么能这样做?

1 个答案:

答案 0 :(得分:2)

不应该手动触发该事件。应该由交互本身来触发它。我会做的是听取不同类型的事件。

ol.interaction.Select有一个功能集合,即ol.Collection。当交互选择功能时,该对象会触发addremove事件,即通过其自己的处理程序的结果,手动推送/删除的结果。

这是一个片段:

var featuresCollection = selection.getFeatures();
featuresCollection.on('add', function() {
  // do what you want on add
});
featuresCollection.on('remove', function() {
  // do what you want on remove
});

这需要更多的管理,因为回调方法是每个功能 ,但最终你可以做你想做的事。