drawend 后自动 select 功能

automatically select feature after drawend

我在 openlayers 3 (v3.9.0) 中有一个 SelectDraw 交互,我想向它添加一些独特的行为。目前,在绘制了 feature 之后,我必须点击 featureselect 它。有没有一种方法可以完全绕过 click 事件,并在 drawend 上让功能自动 select 本身?

谢谢

您只需在 ol.interaction.Select 上调用 getFeatures(),然后将新功能添加到此可观察集合中:

selectCtrl = new ol.interaction.Select();
drawCtrl = new ol.interaction.Draw();

drawCtrl.on("drawend",function(e){
      selectCtrl.getFeatures();
      features.push(e.feature);
});

解决了。 ol.interaction.selectdraw.on('drawend',()) 自行解决后触发。诀窍是在添加新功能后强制 select.condition 为 return false。有关详细信息,请参阅我的 jsfiddle 中 selectedFeature.push(evt.feature)var featureadded 的用法。

http://jsfiddle.net/williemaddox/0um2ud3v/

如果你和我一样,也想在绘图结束后自动离开绘图模式并return进入选择模式(并且选择了特征),你可以这样做:

mySelect = new ol.interaction.Select();
myDraw = new ol.interaction.Draw();
lastDrawnFeature = null;

myDraw.on('drawend',function(e){
    lastDrawnFeature = e.feature;

    // switch to select interaction
    myDraw.setActive(false);
    mySelect.setActive(true);
});

mySelect.on('select',function(e){
    if (lastDrawnFeature) {
        // Actual selection has to be done here,
        // otherwise the last point drawn will be selected instead.
        mySelect.getFeatures();
        features.clear();
        features.push(lastDrawnFeature);
        lastDrawnFeature = null;
    }
});