openlayers绘制矩形/多边形并返回geojson
//添加绘制矩形图层
layer = new vectorLayer({
source: new Vector(),
style: new Style({
stroke: new Stroke({
color: "red",
width: 4,
}),
fill: new Fill({
color: "rgba(255, 255, 255, 0.8)",
}),
}),
})
map.addLayer(layer)
draw = new Draw({
source: layer.getSource(),
type: 'Circle',//矩形绘制
// type: 'Polygon',多边形绘制
geometryFunction: createBox()
});
绘制完成事件
draw.on('drawend', function(e) {
// var geom = e.feature.getGeometry();//拿到绘制图形的geometry
map.removeInteraction(draw);//移除绘制状态,一次只绘制一个图形
let featureGeoJson = JSON.parse(new GeoJSON().writeFeature(e.feature));//将绘制图层转为geojson
console.log(featureGeoJson);
})