vue中leaflet自动清除上一个矩形,并显示坐标

data中数据

  data() {
    return {
      map: null,
      option: {
        //leaflet.pm配置项
        position: "topleft",
        drawPolygon: false, // 添加绘制多边形
        drawMarker: false, //添加按钮以绘制标记
        drawCircleMarker: false, //添加按钮以绘制圆形标记
        drawPolyline: false, //添加按钮绘制线条
        drawRectangle: true, //添加按钮绘制矩形
        drawCircle: false, //  添加按钮绘制圆圈
        editMode: true, //  添加按钮编辑多边形
        dragMode: true, //   添加按钮拖动多边形
        cutPolygon: false, // 添加一个按钮以删除图层里面的部分内容
        removalMode: true, // 清除图层
      },
      rectangle: null,
      rectangleBounds: null
    };
  },

    methods中的代码

    <script>
FirstCesium() {
      // 初始化二维地图
      if (this.map != null) this.map.remove();
      const map = L.map("bigdrawMap", {
        minZoom: 2,// 地图最小缩放层级
        maxZoom: 6,// 地图最大缩放层级,
        //crs: L.CRS.EPSG3857,
        crs: L.CRS.EPSG4326,
        tms:true,
        center: [40.6, 116.8],// 地图中心
        zoom: 3,// 地图默认缩放层级
        attributionControl: false,// 属性控制是否默认加载在地图上
        preferCanvas: true, //使用canvas模式渲染矢量图形
      });
      this.map = map;
      this.map.setView([14.550339, 112.114129], 2);
      L.tileLayer(baseURL.ytbaseMapUrl, {
        minZoom: 2,
        maxZoom: 6,
        tms: true,
      }).addTo(this.map);
      this.map.pm.setLang("zh");
      this.map.pm.addControls(this.option);
      map.invalidateSize();
    },
    // 坐标转换函数
    coordinateTransformation() {
      // 绘图后坐标获取
      this.map.on("pm:create", (e) => {
        if (e.shape == 'Rectangle') {
          // 把当前矩形信息储存到data中的数据
          // console.log("eeeee",e)
          this.rectangle = e.layer;
          this.rectangleBounds = e.layer.getBounds();
        }
        if (e.shape == "Circle") {
          // console.log(e.layer._latlng, e.layer._radius,1)
        } else {
            //这下面的就是矩形坐标信息
           console.log(e.layer._latlngs)
        }
      });
        this.map.on('pm:remove', () => { // 移除矩形时触发
        this.rectangle = null;
        this.rectangleBounds = null;
      });
    },
    drawRectangle() {
      // 每次点击,前面的矩形会自动清除掉
    if (this.rectangle) {
      this.map.removeLayer(this.rectangle);
      this.rectangle = null;
      this.rectangleBounds = null;
    }
    // 自定义绘制矩形点击事件
    this.map.pm.enableDraw("Rectangle", {
        snappable: true,
        snapDistance: 20,
      });
    },
<script/>

mounted()中

  mounted() {
    this.FirstCesium();
    this.coordinateTransformation();
  },

注:drawRectangle()函数是click事件,随意绑定到哪个按钮上都行,按钮是绘制按钮。初始化二维地球弄自己的,不要盲目粘贴。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值