vue + 百度地图GL版判断一个点位是否在地图可视区内

利用BMapGLLib中isPointInRect

因为没有找到官方文档因此直接下载了该工具的源码,复制以下部分到自己的项目中,避免再次引用完整的BMapGLLib脚本

 关键方法

    isPointInRect(point, bounds) {
      if (
        !(point.toString() === "Point" || point.toString() === "LatLng") ||
        !(bounds instanceof BMapGL.Bounds)
      ) {
        return false;
      }
      var sw = bounds.getSouthWest(); // 西南脚点
      var ne = bounds.getNorthEast(); // 东北脚点
      return (
        point.lng >= sw.lng &&
        point.lng <= ne.lng &&
        point.lat >= sw.lat &&
        point.lat <= ne.lat
      );
    },

使用

   isPointInViewport(params = {}) {
      const {sw, ne, point} = params;
      const swGL = new BMapGL.Point(sw.lng, sw.lat);
      const neGL = new BMapGL.Point(ne.lng, ne.lat);
      const pointGL = new BMapGL.Point(point.lng, point.lat);
      if (this.isPointInRect(pointGL, new BMapGL.Bounds(swGL, neGL))) {
        console.log("在区域内");
        return true
      } else {
        console.log("不再区域内");
        return false
      }
    },

    searchRadarInViewport(config) {
      // 参考:
      // this.bdMap = new BMapGL.Map("map-container"); // 创建Map实例
      // let bounds = this.bdMap.getBounds(); //获取地图可视区域


      console.log(config, "config"); // config 是从其他组件接收到的地图bounds
      this.isPointInViewport({
        sw: config.WS,
        ne: config.EN,
        point: {lng: 111.75963626067835, lat: 41.57808734176302}
      })
    },

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值