vue2+高德地图绘制多个圆形覆盖物和多边形覆盖物

文章展示了如何在Vue2项目中结合高德地图API,根据用户交互(如复选框切换)动态绘制和移除圆形及多边形覆盖物。通过监听地图的移动事件,更新覆盖物,并在每次请求后清理旧的覆盖物以防止重复绘制导致的性能问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

vue2+高德地图绘制圆形覆盖物以及多边形覆盖物

直接贴代码

  1. 添加复选款
 <el-checkbox v-model="checkedAri" @change="banAriFly">查看限飞区</el-checkbox>
  1. 为复选款添加事件
banAriFly () {
      // console.log(this.map, "这个事点击的")
      // console.log("这个是未添加括号的:" + this.mapMoveend, "这个是添加括号的:" + this.mapMoveend())
      if (this.checkedAri) {
        //绑定事件(不加括号绑定的就是一个函数,加了括号就是直接执行了那个函数)
        this.map.on('movestart', this.mapMovestart);
        this.map.on('mapmove', this.mapMove);
        this.map.on('moveend', this.mapMoveend)
      } else {
        //解除绑定事件(不加括号绑定的就是一个函数,加了括号就是直接执行了那个函数)
        this.map.off('movestart', this.mapMovestart);
        this.map.off('mapmove', this.mapMove);
        this.map.off('moveend', this.mapMoveend)
        if (this.circlesList.length > 0) {
          for (var i = 0; i < this.circlesList.length; i++) {
            let circle = this.circlesList[i]
            this.map.remove(circle);
          }

        }
        if (this.polygonList.length > 0) {
          for (var i = 0; i < this.polygonList.length; i++) {
            let polygon = this.polygonList[i]
            this.map.remove(polygon);
          }
        }
      }
    },

  1. 接口请求封装
    //接口请求数据
    getData (params) {
      let url = "you request api";
      return axios.get(url, { params: params }).then(response => {
        return response.data;
      })
    },
  1. 调用接口,根据接口请求返回的数据进行实例化覆盖物对象并添加到地图
 //地图移动结束事件s
    mapMoveend () {
      console.log("地图移动结束")
      console.log(this.center.split(","))
      this.ariPlane.lat = this.center.split(",")[1]
      this.ariPlane.lng = this.center.split(",")[0]
      console.log(this.ariPlane, "这个是打印经纬度")
      const params = {
        lng: this.ariPlane.lng,
        lat: this.ariPlane.lat,
      }

      this.getData(params).then(res => {
        // 数据渲染前,先把上一次赋值的进行清空操作
        if (this.circlesList.length > 0) {
          for (var i = 0; i < this.circlesList.length; i++) {
            let circle = this.circlesList[i]
            this.map.remove(circle);
          }

        }
        if (this.polygonList.length > 0) {
          for (var i = 0; i < this.polygonList.length; i++) {
            let polygon = this.polygonList[i]
            this.map.remove(polygon);
          }
        }
        this.circlesList = []
        this.polygonList = []
        this.ariPlaneAreas = res.areas
        this.ariPlaneAreas.map((item) => {
          if (item.sub_areas == null || item.sub_areas.length == 1) {
            // 创建圆形覆盖物
            let circleItem = this.createCircle(item)
            this.circlesList.push(circleItem)
          } else {
            item.sub_areas.map(items => {
              let polygonItem = this.createPolygon(items)
              this.polygonList.push(polygonItem)

            })
          }
        })

        if (this.circlesList.length > 0 && this.polygonList.length > 0) {
          console.log("circlesList=", this.circlesList)
          this.map.add(this.circlesList)
          this.map.add(this.polygonList)
        }
      });
    },
  1. 封装圆形覆盖物以及多边形覆盖物实例函数

    // 创建圆形 
    createCircle (data) {
      this.circle = new AMap.Circle({
        center: [data.lng, data.lat],
        radius: data.radius,
        strokeColor: data.color,
        strokeWeight: 2,
        strokeOpacity: 0.5,
        fillColor: data.color,
        fillOpacity: 0.35
      });
      return this.circle;
    },

    //创建多边形
    createPolygon (data) {
      let pathPolygon = data.polygon_points[0]
      this.polygon = new AMap.Polygon({
        path: pathPolygon,//设置多边形边界路径
        strokeColor: data.color, //线颜色
        strokeOpacity: 0.5, //线透明度
        strokeWeight: 2,    //线宽
        fillColor: data.color, //填充色
        fillOpacity: 0.35//填充透明度
      });
      return this.polygon;
    },

6.注意每次请求发完以后获取到的数据将覆盖物画完以后需要清除掉,不然每次请求的数据有重复的返回数据就会导致界面所画的覆盖物重复绘画导致一个覆盖物绘制多次,使得界面发现卡顿现象

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值