如何在drawend添加到OL3中的源代码后捕获事件?

hyp*_*not 5 openlayers-3

我想在将每个要素绘制到地图后更新列表.

当我drawend用来捕捉绘图的完成时,当时正在绘制的特征尚未添加到矢量源.

所以

var draw = new ol.interaction.Draw({
    source: source,
    type: 'Point'
});

draw.on('drawend', function () {
    console.log(source.getFeatures().length)
});

map.addInteraction(draw);
Run Code Online (Sandbox Code Playgroud)

添加第一个点时输出0.

如何在绘图完成并将特征添加到矢量源时捕获地图的状态?因此,当空映射上的source.getFeatures().length为1时,我正在寻找一个状态.

pav*_*los 10

你可以随时尝试@jonatas的建议.它应该完成你要找的工作.另一个解决方法是从事件本身获取当前绘制的功能,并将其添加到功能数组中.检查一下

draw.on('drawend', function (e) {
var currentFeature = e.feature;//this is the feature fired the event
var restOfFeats = source.getFeatures();//rest of feats
var allFeats = restOfFeats.concat(currentFeature);//concatenate the event feat to the array of source feats
console.log(allFeats.length)
});
Run Code Online (Sandbox Code Playgroud)