【问题标题】:automatically select feature after drawend绘制结束后自动选择特征
【发布时间】:2015-10-13 22:12:25
【问题描述】:

我在 openlayers 3 (v3.9.0) 中有 SelectDraw 交互,我想为其添加一些独特的行为。目前,在绘制feature 后,我必须单击featureselect。有没有办法完全绕过click 事件并让功能在drawend 上自动选择?

谢谢

【问题讨论】:

  • 选择一个正确的答案,不要一直悬而未决。

标签: javascript openlayers-3


【解决方案1】:

您只需在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);
});

【讨论】:

  • 这仅在触发drawend时光标直接位于特征上方时有效。
【解决方案2】:

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

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

【讨论】:

    【解决方案3】:

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

    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;
        }
    });
    

    【讨论】:

      猜你喜欢
      • 2019-02-09
      • 2021-07-26
      • 2012-12-17
      • 1970-01-01
      • 2013-02-21
      • 2017-02-10
      • 2017-06-03
      相关资源
      最近更新 更多