这里用的elementui
<el-button-group>
<el-button type="primary" @click="drawVector('Point')">画点</el-button>
<el-button type="primary" @click="drawVector('LineString')">画线 </el-button>
<el-button type="primary" @click="drawVector('Polygon')">画面</el-button>
<el-button type="primary" @click="drawVector('Circle')">画圆</el-button>
<el-button type="primary" @click="drawVector('None')">清除</el-button>
</el-button-group>
主要步骤:
drawVector(type) {
this.map.removeInteraction(this.draw);
if (type !== 'None') {
console.log(type);
this.addInteraction(type);
} else {
//清空绘制图形
this.map.getAllLayers().forEach((element) => {
console.log(element.values_.name);
if (element.values_.name == 'drawtest') {
console.log(element.values_.name);
this.map.removeLayer(element);
}
});
}
},
主要步骤:
//根据绘制类型进行交互绘制图形处理
addInteraction(type) {
//实例化一个矢量图层Vector作为绘制层
this.vector = new VectorLayer({
name: 'drawtest',
source: new VectorSource({ wrapX: false }),
style: new Style({
fill: new Fill({
color: 'rgba(255, 255, 255, 0.5)',
}),
stroke: new Stroke({
color: '#e60039',
width: 2,
}),
image: new CircleStyle({
radius: 7,
fill: new Fill({
color: '#e60039',
}),
}),
}),
zIndex: 10,
});
//将绘制层添加到地图容器中
this.map.addLayer(this.vector);
this.draw = new Draw({
source: this.vector.getSource(),
type: type,
});
this.map.addInteraction(this.draw);
},
老靳的WebGIS
”回复ol06
获取1 使用vscode打开
2 使用npm i 命令安装引用的库
3 使用npm run serve 命令运行程序