javascript - 替换 'drawend' 上绘制的几何图形

标签 javascript openlayers-3

我想做的是在绘制特征后替换或更改特征的几何形状。例如:我画一条线,画完后,我用 Turf.js 修改几何图形,在绘制的线周围创建一个缓冲区,并显示缓冲的线(多边形)而不是线。

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

draw.on('drawend', function(e) {

    //Sending the LineString to a Turf function with a buffer value and getting a Polygon in return
    var bfc = bufferedFeatureCollection(100, e.feature);

    //Replacing the LineString geometry with a Polygon geometry
    e.feature.setGeometry(bfc[0].getGeometry());

    //Removes and terminates the draw action.... 
    map.removeInteraction(this);
});

现在从 console.log() 中,我可以看到 feature.geometry 已从 ol.geom.linestring 更改为ol.geom.polygon。但在 map 上我仍然看到显示一条线。

我做错了什么?

最佳答案

将功能添加到 ol.source.Vector 后进行这些修改,所以:

vectorSource.on('addfeature', function(evt){
    //Sending the LineString to a Turf function with a buffer value and getting a Polygon in return
    var bfc = bufferedFeatureCollection(100, evt.feature);

    //Replacing the LineString geometry with a Polygon geometry
    evt.feature.setGeometry(bfc[0].getGeometry());

    //Removes and terminates the draw action.... 
    map.removeInteraction(draw);
});

关于javascript - 替换 'drawend' 上绘制的几何图形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34964375/

相关文章:

javascript - OL3 : force redraw layer

javascript - 我怎样才能在将图层添加到 map 时将其添加为 OpenLayers 3 中的底层?

javascript - 如何选择 <tr> 中第一个和最后一个出现的 <td>?

javascript - 如何获取按钮的数据属性并显示该页面?

javascript - 无法使用 OpenLayers 3 站点中的 WFS 示例作为基础加载另一个 WFS

javascript - 开放层 3 : Drawing a polygon feature over the dateline

javascript - 使用 canvas.toDataURL 时如何设置 crossOrigin 属性?

javascript:这段代码的完整语法是什么?

javascript - 将字符串转换为整数时出现错误

javascript - HTML 按钮的 "onclick"属性在单击之前调用函数