如何在打开的图层中删除选定的形状

How to delete a selected shape in openlayers

本文关键字:删除 图层      更新时间:2023-09-26

我目前正在看到这个开放层(http://openlayers.org/en/v3.14.2/examples/draw-features.html)的例子,它在地图上画线,圆圈等。我编辑了代码以便能够选择图纸。我想知道如何删除选定的图纸?以下是我选择图纸的方式:

var select = new ol.interaction.Select();

有什么想法吗?谢谢!

选择

要素后,可以通过将其从图层中移除来删除该要素。对交互中选择的要素使用"添加"侦听器,然后将其从图层源中删除。用

select = new ol.interaction.Select();
select.getFeatures().on('add', function(feature){
  //source is layer.getSource()
  source.removeFeature(feature.element);
  feature.target.remove(feature.element);
});
map.addInteraction(select);

JSFiddle 链接(在下拉列表中选择"删除"以选择和删除功能):http://jsfiddle.net/anushamc/edms856o/