OpenLayers6(6):绘制图形工具条封装(Draw、Snap、Modify)

1 版本

  • OpenLayers:6.14.1


2 说明

  • Draw:绘制图形
  • Snap:捕捉图形
  • Modify:修改图形

3 绘制图形组件

将绘制界面直接封装成vue单文件组件,上代码:

<template>
  <div class="ol-draw-collapse">
    <el-collapse :value="['1']">
      <el-collapse-item name="12">
        <template slot="title">
          <p-button class="t-btn" tip="图形绘制" icon="draw" :place="pPlace" />
        </template>
        <p-button tip="点绘制" icon="point" :place="pPlace" @click="startDraw('Point')" />
        <p-button tip="线绘制" icon="polyline" :place="pPlace" @click="startDraw('LineString')" />
        <p-button tip="面绘制" icon="polygon" :place="pPlace" @click="startDraw('Polygon')" />
        <p-button tip="圆绘制" icon="circle" :place="pPlace" @click="startDraw('Circle')" />
        <p-button tip="完成绘制" icon="done" :place="pPlace" @click="doneDraw()" />
        <p-button tip="清除图形" icon="clear" :place="pPlace" @click="clearDraw()" />
      </el-collapse-item>
    </el-collapse>
  </div>
</template>
 
<script>
// PButton 自己封装的el-button按钮组件
import PButton from "../common/PButton.vue";  
// ODraw 编写的绘制图形的类
import { ODraw } from "@/assets/js/ol/POpenLayers.js";

export default {
  components: { PButton },
  name: "OlDraw",
  props: {
    pMap: {
      default: null,
      require: true,
    },
    pPlace: {
      default: "right",
      require: false,
    },
  },
  data() {
    return {
      mOlDraw: null,
    };
  },

  mounted() {
    this.mOlDraw = new ODraw(this.pMap);
    this.mOlDraw.init();
  },

  methods: {
    startDraw(drawType) {
      this.mOlDraw.startDraw(drawType);
    },

    doneDraw() {
      this.mOlDraw.doneDraw();
    },

    clearDraw() {
      this.mOlDraw.clearDraw();
    },
  },
};
</script>

<style lang='scss'>
@import "~@/assets/css/mixin.scss";
.ol-draw-collapse {
  @include p-collapse;
}
</style>

4 绘制图形类

主要基于动态原型模型编写绘制图形的类ODraw,上代码:

export function ODraw(map) {
    let this_ = this;

    this_.mMap = map;

    this_.mSource = null;
    this_.mLayer = null;

    this_.mDraw = null;
    this_.mModify = null;
    this_.mSnap = null;


    if (typeof ODraw.initialized == "undefined") {

        ODraw.prototype.init = () => {
            this_.mSource = new VectorSource();
            this_.mLayer = new VectorLayer({
                title: "草图图层", // ol-ext图层控件属性
                noSwitcherDelete: true, // ol-ext图层控件属性
                source: this_.mSource, 
            });
            this_.mMap.addLayer(this_.mLayer);


            this_.mModify = new Modify({
                source: this_.mSource,
                style: measureModifyStyle() // 自定义样式
            });
            this_.mMap.addInteraction(this_.mModify);
        };


        ODraw.prototype.addInteractions = (drawType) => {
            this_.mDraw = new Draw({
                source: this_.mSource,
                type: drawType,
            });
            this_.mDraw.on('drawstart', function () {
                this_.mModify.setActive(false);
            });
            this_.mDraw.on('drawend', function () {
                this_.mModify.setActive(true);
            });
            this_.mModify.setActive(true);
            this_.mMap.addInteraction(this_.mDraw);

            this_.mSnap = new Snap({
                source: this_.mSource
            });
            this_.mMap.addInteraction(this_.mSnap);
        };

        ODraw.prototype.removeInteractions = () => {
            this_.mDraw && this_.mMap.removeInteraction(this_.mDraw);
            this_.mSnap && this_.mMap.removeInteraction(this_.mSnap);
        };

        ODraw.prototype.startDraw = (drawType) => {
            this_.removeInteractions();
            this_.addInteractions(drawType);
        };

        ODraw.prototype.doneDraw = () => {
            this_.removeInteractions();
            // this_.mMap.getView().fit(this_.mSource.getExtent());
        };

        ODraw.prototype.clearDraw = () => {
            this_.mSource.clear();
        };

        ODraw.initialized = true;
    }

}

5 实现效果

  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

碰碰qaq

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

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

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

打赏作者

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

抵扣说明:

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

余额充值