繁体   English   中英

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

[英]How to catch event after drawend added to source in OL3?

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

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

所以

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

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

map.addInteraction(draw);

添加第一个点时输出0。

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

你可以随时尝试@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)
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM