如何检索和删除通过交互绘制的形状

时间:2015-06-19 11:30:51

标签: javascript openlayers-3

使用OpenLayers 3.我们有:

var geometryType = 'Circle';
var interactionDraw = new ol.interaction.Draw({
  source: source,
  type: /** @type {ol.geom.GeometryType} */ (geometryType)
});
$scope.map.addInteraction(interactionDraw);

我们抓住'drawend'事件并做一些与此无关的事情,值得一提的是它返回false以消除点击效果。

interactionDraw.on('drawend', function(event){
 //event code
  return false;
};

我们如何访问添加的形状并将其删除,或者阻止它出现?

1 个答案:

答案 0 :(得分:1)

很简单,在交互构造函数上添加collection目的地,然后在drawend处将其删除。

var collection = new ol.Collection();

draw = new ol.interaction.Draw({
    source: source,
    features: collection,
    //...
draw.on('drawend', function(evt){
    console.info(collection.getLength());
    collection.pop();
});

更新 - 尝试进行以下修改:

var collection = new ol.Collection();

draw = new ol.interaction.Draw({
    source: source,
    features: collection,
    //...
});

vectorSource.on('addfeature', function(){
    var feature = collection.item(collection.getLength() - 1);
    source.removeFeature(feature);
    collection.pop();
});