【问题标题】:How to catch event after drawend added to source in OL3?OL3中将drawend添加到源后如何捕获事件?
【发布时间】:2016-08-29 22:28:50
【问题描述】:

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

当我使用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 的状态。

【问题讨论】:

  • 回答here.
  • @JonatasWalker 非常感谢,有趣的是,这两个问题几乎同时被问到。如果可能的话,我建议将其作为一个事件添加到绘图交互中。

标签: openlayers-3


【解决方案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)
});

【讨论】:

  • 这不会导致重复功能吗?
  • 每次 'drawend' 事件触发时,您都会拥有一系列新功能 (allFeats)。没有重复。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多