利用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 是从其他组件接收到的地图boundsthis.isPointInViewport({sw: config.WS,ne: config.EN,point: {lng: 111.75963626067835, lat: 41.57808734176302}})},