2015-06-19 35 views
2

使用的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

很簡單,在交互構造函數中添加一個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(); 
}); 
+0

謝謝你,但是這deosn't防止被繪製圖紙,並沒有將其刪除。 – TTT

+0

它會刪除,您可以使用'collection.getLength()'進行檢查。我正在尋找圖層刷新。 –

+0

你是對的,它只從'ol.Collection'中移除,現在它也從源代碼中移除。 –

相關問題