首页 > 其他分享 >百度地图(BMapGL) 显示可视范围的插件 mapMaxBounds

百度地图(BMapGL) 显示可视范围的插件 mapMaxBounds

时间:2022-08-17 11:03:59浏览次数:48  
标签:function map 插件 bounds mapMaxBounds oldZoom addEventListener BMapGL zoomend

class mapMaxBounds {
  
  
// map 是百度的BMap实例对象
// bounds 是百度的可视范围类型 BMap.bounds
// 这里的map类型在我开发的时候使用的是BMapGL
constructor(map, bounds) {
this.map = map;
        map.disableInertialDragging();
        this.enableMaxBounds(bounds);
    }

    // 关闭限制区域
    disableMaxBounds() {
        this.map.container.removeEventListener("mouseup", this.containerMounseup);
        this.map.removeEventListener("moving", this.moving);
        this.map.removeEventListener("zoomstart", this.zoomstart);
        this.map.removeEventListener("zoomend", this.zoomend);
    }

    // 开启限制最大范围
    enableMaxBounds(bounds) {
        // 开启监听
        // var extent=new Object();
        // extent.minX="-180.380133";
        // extent.maxX="177.557674";
        // extent.minY="-80.395352";
        // extent.maxY="86"; 

        let _this = this;

        let oldPoint = null;
        let oldBounds = null, oldCenter = null, oldZoom = null;

        // this.containerMousedown = (e)=>{  };
        // this.map.container.addEventListener("mousedown", this.containerMousedown);

        this.containerMounseup = (e) => { this.map.enableDragging(); }
        this.map.container.addEventListener("mouseup", this.containerMounseup);

        // this.movestart = function(){ }
        // this.map.addEventListener("movestart", this.movestart);

        // this.dragend = function(){ console.log("dragend"); }
        // this.map.addEventListener("dragend", function(){ console.log("dragend"); });

        this.moving = function (type, target) {
            if (!bounds.containsBounds(this.getBounds())) {

                if (oldPoint) {

                    this.panTo(oldPoint, { noAnimation: false });
                }
                this.disableDragging();
            } else {
                oldPoint = this.getCenter();
            }
        }
        this.map.addEventListener("moving", this.moving);

        this.zoomstart = function () {
            if (!map.config.enableWheelZoom) return;
            oldBounds = this.getBounds();
            oldCenter = this.getCenter();
            oldZoom = this.getZoom();
        }
        this.map.addEventListener("zoomstart", this.zoomstart);

        const keyZoom = true;

        this.zoomend = function (type, target) {

            if (!map.config.enableWheelZoom) return;

            if (!bounds.containsBounds(this.getBounds())) {
                this.disableScrollWheelZoom();
                setTimeout(() => {
                    this.enableScrollWheelZoom()
                }, 1000)
                // oldBounds && this.setViewport([oldBounds.sw,oldBounds.ne], { enableAnimation: false, delay: 0, zoomFactor: -1});
                if (oldZoom < 4.4 || this.getZoom() < 4.6) {
                    // this.setBounds(bounds);
                    // this.panTo(new BMapGL.Point(106.75431201486032, 34.13381700418861), { noAnimation: false });
                    // this.setZoom(4.5);
                    this.setViewport({ center: new BMapGL.Point(106.75431201486032, 34.13381700418861), zoom: 4.5 })
                }
                else if (this.getZoom() != oldZoom) {
                    //this.panTo(oldCenter, { noAnimation: false });
                    //this.setZoom(oldZoom);
                    //this.centerAndZoom(oldCenter, oldZoom);
                    this.setViewport({ center: oldCenter, zoom: oldZoom });
                }
            };
        }
        this.map.addEventListener("zoomend", this.zoomend);

        this.map._off = this.map.off;
        this.map.off = function (type) {
            this._off.apply(this, arguments);
            if (type === "zoomend") {
                this.addEventListener("zoomend", _this.zoomend);
            }
        }
    }
}

标签:function,map,插件,bounds,mapMaxBounds,oldZoom,addEventListener,BMapGL,zoomend
From: https://www.cnblogs.com/liao1992/p/16594295.html

相关文章

  • flutter 技术选型 —— 视频播放器插件
    chewie依赖:video_player+chewie缺点:ui简陋,无法后台播放classVideoPlayerScreenextendsStatefulWidget{constVideoPlayerScreen({Key?key}):super(key:ke......
  • VS Code常用插件
    VSCode常用插件一、插件的下载打开VScode之后点击右侧带有小方块的图标在上方的输入框中输入想要下载的插件的名称即可二、插件的种类Chinese(Simplified)Langu......
  • Vue+Leaflet.PM插件实现创建和编辑几何图形(点、线、面、圆等)
    场景Vue+Leaflet实现加载OSM显示地图:https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/122317394在上面加载显示OSM的基础上,使用Leaflet.pm插件实现在页面上......
  • 备份谷歌或其他浏览器插件
    原文链接备份谷歌或其他浏览器插件1浏览器地址栏输入:chrome://extensions/,选择开发模式,单击打包扩展程序,可以看到一个“扩展程序根目录”输入框。2进入浏览器插件的......
  • rabbitmq延时插件的安装
    目录rabbitmq延时插件的安装1下载具体操作2将插件拷贝至容器中3进入容器,并查看插件4启动插件并重启容器5查看是否生效相关文档rabbitmq延时插件的安装资料参考地址......
  • 开发Chrome插件,实现网站自动登录
    近期被一个事情困扰着,我们采购了一款软件,里面有一个数据大屏页,当登录过期后,数据就会保持原状,不再更新。和供应商反馈了很多次,都无法彻底解决数据显示的问题,没办法,自己......