OpenLayers 实现画点、画线、画面、画圆

本文将实现如何用openlayers在地图上画点、线、面、圆,创建地图可参考我的上一篇文章Vue+OpenLayers 创建地图并显示鼠标所在经纬度

1、页面

OpenLayers 实现画点、画线、画面、画圆_第1张图片

2、代码

                  <div class="fense_card_top_flex">
          <button @click="selectDraw('Point')"></button>
          <button @click="selectDraw('LineString')">线</button>
                   <button @click="selectDraw('Polygon')">多边形</button>
                 <button @click="selectDraw('Circle')"></button>                
        </div>
  import VectorSource from "ol/source/Vector";
import VectorLayer from "ol/layer/Vector";
import {  Draw } from "ol/interaction";

     selectDraw(value) {
        //取消上一次的绘制动作,避免重叠
        this.draw ? this.map.removeInteraction(this.draw) : '';

        var source = new VectorSource();
        //定义vector图层,将绘制的图形加入到vector图层,叠加在地图上展示
        var vector = new VectorLayer({
          source: source
        });

        if (value === 'Polygon') {

          this.draw = new Draw({
            source: source,
            type: value,
            geometryFunction: Draw.createBox
          });
        } else {
          this.draw = new Draw({
            source: source,
            type: value
          });
        }
        // 将交互动作添加到地图上
        this.map.addInteraction(this.draw);
        this.map.addLayer(vector);


      },

你可能感兴趣的:(OpenLayers,vue.js)