绘制完成后如何通过绘制交互删除特征?

时间:2021-04-13 06:09:46

标签: openlayers

当我通过绘制交互完成绘制时,地图上会显示该要素。

我不希望地图上显示该要素。

我想在绘制完成后删除该功能。

我从 drawend 事件中删除了该功能,但它仍然显示在地图上。

什么时候应该删除该功能?

var draw = new ol.interaction.Draw({
            source: source,
            type: 'LineString',
            style: style,           
            freehandCondition: function freehandCondition(e) {
                return false;
            }
});

map.addInteraction(draw);

drawInteraction.on('drawend', function (e) {
    source.removeFeature(e.feature); // not work
});

2 个答案:

答案 0 :(得分:1)

仅当您指定要添加它们的源时才会添加绘图功能,如果您不想保存它们,则不需要源

var draw = new ol.interaction.Draw({
            type: 'LineString',
            style: style,           
            freehandCondition: function freehandCondition(e) {
                return false;
            }
});

map.addInteraction(draw);

答案 1 :(得分:0)

我用 addFeature 事件解决了这个问题。

source.on("addfeature", function(e){    
                if(e.feature == global_feature)
                {
                    source.removeFeature(e.feature);
                }                           
        });