解决使用Arcgis for API Javascript中的Draw画完之后无法清除画布问题

一、绘制巡检区域

    //绘制巡检区域
    drawArea() {
      loadModules(
        [
          'esri/Graphic',
          'esri/views/draw/Draw',
          'esri/geometry/Point',
          'esri/geometry/Polyline',
          'esri/geometry/Polygon'
        ])
        .then(([Graphic, Draw, Point, Polyline, Polygon]) => {
          this.isDrawing = true

          this.view.focus()

          const draw = new Draw({
            view: this.view
          })


          const action = draw.create('polygon', { mode: 'click' })
          this.action = action
          action.on(['vertex-add', 'vertex-remove'], (event) => {
            //少于两点无法展示线
            let vertices = event.vertices
            if (vertices.length < 2) {
              return
            }

            //绘制测距线,并将其添加到地图
            var polyLine = new Polyline({
              paths: vertices,
              spatialReference: this.view.spatialReference
            })
            const graphic = new Graphic({
              geometry: polyLine,
              symbol: {
                type: 'simple-line',
                cap: 'round',
                join: 'round',
                color: '#14D3C6'
              }
            })
            this.graphicsLayer.add(graphic)
          })

          action.on(['draw-complete'], (event) => {
            //生成绘制的图形
            let vertices = event.vertices
            let polygon = new Polygon({
              rings: vertices,
              spatialReference: this.view.spatialReference
            })
            let graphic = new Graphic({
              geometry: polygon,
              symbol: {
                type: 'simple-fill',
                color: [171,249,229,0.5],
                style: 'solid',
                outline: {
                  color: '#14D3C6',
                  width: 1
                }
              }
            })
            this.graphicsLayer.add(graphic)

            //添加巡检区域坐标
            for (let i = 0; i < vertices.length; i++) {
              let point = new Point({
                x: vertices[i][0],
                y: vertices[i][1],
                spatialReference: this.view.spatialReference
              })
              if (!this.form.hasOwnProperty('gpsAreaZbList')) {
                this.form.gpsAreaZbList = [];
              }
              this.form.gpsAreaZbList.push({
                weidu: Number(point.latitude).toFixed(8),
                jingdu: Number(point.longitude).toFixed(8),
                sOrder: i + 1,
                type: 0
              })
            }
            this.isDrawing = false
            // 禁用绘制工具
            action.destroy();
          })
        })
    },

二,清除图层(注意得用destroy)

    //清除
    clear() {
      //删除巡检区域坐标和关键点坐标数据
      this.form.gpsAreaZbList = []
      //清除图层
      this.graphicsLayer.removeAll()
      this.action.destroy();
    },

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值