替换“绘制”上绘制的几何图形
Replacing the drawn geometry on 'drawend'
我要做的是在绘制特征后替换或更改其几何形状,例如:我画了一条线,画完后,我用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
。但是在地图上,我仍然看到一条线正在显示。
我做错了什么?
在将功能添加到 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);
});
我犯的错误是缓冲区值太低而无法被视为缓冲区,因此我只看到了一条线(即使它是多边形),我不得不从100 to 10000000
提高缓冲区值......
因此,在draw.on('drawend')
和vectorSource.on('addfeature')
中都可以替换已绘制的几何图形。
编辑:
为了答案的完整性。我不得不从100 to 10000000
更改缓冲区的原因是我忘记将几何转换为EPSG:4326
,然后再将其传递给 Turf 函数。草皮仅适用于EPSG:4326
。
var draw = new ol.interaction.Draw({
source: vectorSource,
type: 'LineString'
});
draw.on('drawend', function(e) {
e.feature.getGeometry().transform('EPSG:3857', 'EPSG:4326');
var bfc = bufferedFeatureCollection(radius, e.feature);
bfc[0].getGeometry().transform('EPSG:4326', 'EPSG:3857');
e.feature.setGeometry(bfc[0].getGeometry());
//Removes and terminates the draw action....
map.removeInteraction(this);
});
相关文章:
- D3在一个调用中绘制不同的SVG形状,没有可见性
- 有没有一个javascript图形绘制库可以进行气球树布局
- 使用相同的数据集绘制各种符号
- 使用onclick绘制SVG路径
- 在谷歌地图上绘制位置数据库
- 用chart.js绘制条形图
- “可绘制地图”设置地图选项“纬度-经度”
- 多维数据集网格未在指定的分区中绘制
- 使用谷歌图表在x轴上绘制日期
- 具有大型几何图形的基于沙发的空间查询
- 使用fabric.js从矩形区域获取对象,并将该区域绘制到画布上
- 如何在React Native中绘制图形
- 画布中绘制的矩形区域的弹出工具提示
- 如何在OpenLayers中获取动态绘制的多边形的坐标
- 画布:逐像素绘制图像并请求动画帧计时
- D3从嵌套的JSON中绘制第二个圆环图
- 如何用d3.js绘制折线图
- 如何绘制具有tween动画效果的三js线几何图形
- 替换“绘制”上绘制的几何图形
- 是否可以在HTML5.canvas中绘制大量简单的几何图形