【开源WebGIS】06-Openlayers+Vue 画点、线、面

openlayers画点、线、面

实现效果

在这里插入图片描述

实现步骤

1 定义几个按钮按钮

这里用的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>

2 定义按钮的点击事件

主要步骤:

  1. 清除Interaction
  2. 判断按钮类型
  3. 如果是画东西的按钮就执行addInteraction函数,来画点、线、面
  4. 如果是清除图形的按钮,就先获取map的所有layers,然后根据name删除前面画的点、线、面
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);
          }
        });
      }
    },

3 addInteraction绘制图形

主要步骤:

  1. 定义一个vector作为layer
  2. 给其添加一个name属性,便于后面可以按照name清除图形
  3. 指定其style属性
  4. 指定其zIndex属性大于9,因为我这个osm的底图默认的zInex居然是9,所以我画的图形都被osm底图盖住看不到了
  5. 使用map.allLyaer()将vector添加到地图中
  6. 定义一个Draw,指定其source和type(既点、线或者面)
  7. 将addInteraction添加至map中
//根据绘制类型进行交互绘制图形处理
    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);
    },

获取代码

  1. 关注公粽号“老靳的WebGIS”回复ol06获取
1 使用vscode打开
2 使用npm i 命令安装引用的库
3 使用npm run serve 命令运行程序
  • 0
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

老靳的WebGIS

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值