openlayers获取绘制多边形的顶点坐标

虽使用Interaction无数次,进行图形绘制与用户交互等,但当需要获取绘制图形的顶点坐标时还是不晓得咋弄?

都知道在绘制完成后回调中能获取到当前的event对象draw.on('drawend', function(e) {}) 而这个对象中就能拿到feature,根据这个就可以找到如下feature api , 踏又可以通过getGeometry得到对应的polygon

在这里插入图片描述
根据上面获得了polygon, so再找到polygon api ,他就有一个getCoordinates的方法
在这里插入图片描述

根据上面顺藤摸瓜就可以得出如下操作—>
在这里插入图片描述


具体操作方法是这样的


1、画笔初始化方法
    /**
     * 画笔初始化
     */
    drawPrepare() {
      const source = new VectorSource()
      const vector = new VectorLayer({
        source: source,
        style: new Style({
          fill: new Fill({
            color: 'rgba(255,218,185, 0.4)'
          }),
          stroke: new Stroke({
            color: '#ffcc33',
            width: 2
          }),
          image: new Circle({
            radius: 7,
            fill: new Fill({
              color: '#ffcc33'
            })
          })
        })
      })
      this.map.addLayer(vector)

      var modify = new Modify({
        source: source
      })
      this.map.addInteraction(modify)
      this.sourceOfPolygon = source
    },    


2、执行绘制方法
/**
     * 执行绘制
     */
    drawPattern() {
      const _self = this
      const source = this.sourceOfPolygon
      const draw = new Draw({
        source: source,
        type: 'Polygon'
      })

      // 添加 interaction
      this.map.addInteraction(draw)
      const snap = new Snap({
        source: source
      })

      // 添加 snap
      this.map.addInteraction(snap)

      draw.on('drawend', function(e) {
        const geometry = e.feature.getGeometry()
        const corrdinates = geometry.getCoordinates()
        console.log(corrdinates)
        // 清除画笔
        _self.map.removeInteraction(draw)
        _self.map.removeInteraction(snap)
      })
    }
  }

ok 顶点坐标获取到了接下来就是业务逻辑了…

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

奔跑的痕迹

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

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

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

打赏作者

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

抵扣说明:

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

余额充值