如何检索和删除通过交互绘制的形状
How can I retrieve and remove a shape that has been drawn through interaction
使用 OpenLayers 3。我们有:
var geometryType = 'Circle';
var interactionDraw = new ol.interaction.Draw({
source: source,
type: /** @type {ol.geom.GeometryType} */ (geometryType)
});
$scope.map.addInteraction(interactionDraw);
我们捕获 'drawend' 事件并做一些这里无关紧要的事情,值得一提的是它 returns false 消除了点击效果。
interactionDraw.on('drawend', function(event){
//event code
return false;
};
我们如何访问添加的形状并将其删除,或者完全阻止它出现?
很简单,在交互构造函数上添加一个 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();
});
使用 OpenLayers 3。我们有:
var geometryType = 'Circle';
var interactionDraw = new ol.interaction.Draw({
source: source,
type: /** @type {ol.geom.GeometryType} */ (geometryType)
});
$scope.map.addInteraction(interactionDraw);
我们捕获 'drawend' 事件并做一些这里无关紧要的事情,值得一提的是它 returns false 消除了点击效果。
interactionDraw.on('drawend', function(event){
//event code
return false;
};
我们如何访问添加的形状并将其删除,或者完全阻止它出现?
很简单,在交互构造函数上添加一个 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();
});