我想要做的是在繪製後替換或更改要素的幾何圖形。例如:我繪製一條線,繪製完成後,我使用Turf.js
修改幾何圖形,在繪製線周圍製作緩衝區,並顯示緩衝線(多邊形)而不是線。替換'drawend'上繪製的幾何圖形
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
。但在地圖上我仍然看到一條線正在顯示。
我在做什麼錯?
嘿,謝謝你的回覆。我發現問題是什麼。你的答案是正確的,我的代碼也可以工作,所以在vectorSource.on('addfeature')和draw.on('drawend')中都可以改變幾何。問題是緩衝區的半徑太小,必須將值從100更改爲10000000,才能看到實際的緩衝區...:S – Stevan