百度地图JavaScript如何清除指定类型的覆盖物

由于一个地图中有很多种类型的覆盖物,由于某个覆盖物(一般是自定义)整个地图中只允许出现一次

那第一想到的就是,每次创建这个类型的覆盖物时先清除这一类型的覆盖物,比较简单判断覆盖物的类型 instanceof ,部分代码如下(红色代码为重点 ):

PoiMarker.prototype = new BMap.Overlay();
                    PoiMarker.prototype.initialize = function (map) {
                        this._map = map;
                        const div = this._div = document.createElement("div");
                        div.style.zIndex = BMap.Overlay.getZIndex(500);
                        div.style.position = "absolute";
                        div.style.background = "url(http://s.xxx.com/images/bcg-green.png) no-repeat bottom";
                        div.style.cursor = "pointer";
                        div.style.height = "42px";
                        const img = document.createElement("img");
                        img.src = this._img
                        div.style.zIndex = BMap.Overlay.getZIndex(400);
                        img.style.height = "36px";
                        img.style.width = "36px";
                        img.style.borderRadius = "26px";
                        img.style.border = "2px solid #72b80a";
                        div.appendChild(img);
                        // POI点击事件
                        div.onclick = (e) => {
                            // 禁止事件冒泡
                            const oEvent = e || event;
                            oEvent.cancelBubble = true;
                            // 删除其它的 poiInfoWindowMarker
                            //获取地图上所有的覆盖物
                            const allOverlay = this._map.getOverlays();
                            for (const item of allOverlay) {
                                if (item instanceof PoiInfoWindowMarker) {
                                    this._map.removeOverlay(item);
                                }
                            }
                            const poiInfoWindowMarker = new PoiInfoWindowMarker(this._point, this._img);
                            // 添加节点数覆盖物到地图上(并将覆盖物注册)
                            this._map.addOverlay(poiInfoWindowMarker);
                        };
                        map.getPanes().labelPane.appendChild(div);
                        return div;
                    };
                    PoiMarker.prototype.draw = function () {
                        const map = this._map;
                        const pixel = map.pointToOverlayPixel(this._point);
                        this._div.style.left = (pixel.x - 18) + "px";
                        this._div.style.top = (pixel.y - 40) + "px";
                    };

                    this.poiMarker = PoiMarker

 

附带一张GIF图解:

 

 

*** 百度地图给出了根据label中的content比较后删除覆盖物(不太符合我需要的场景)

http://lbsyun.baidu.com/jsdemo.htm#c1_17

    function deletePoint(){
        var allOverlay = map.getOverlays();
        for (var i = 0; i < allOverlay.length -1; i++){
            if(allOverlay[i].getLabel().content == "我是id=1"){
                map.removeOverlay(allOverlay[i]);
                return false;
            }
        }
    }

 

转载于:https://www.cnblogs.com/liugx/p/9550148.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
清除百度地图指定覆盖物可以通过以下步骤实现: 1. 首先,确保已经加载了百度地图API,并创建了地图实例。 2. 在地图上绘制了覆盖物后,为了清除指定覆盖物,需要为每个覆盖物添加一个唯一的标识符(通常是一个字符串)。 3. 在需要清除指定覆盖物的时候,通过调用API提供的方法,查找并获取需要清除覆盖物对象。 4. 调用获取到的覆盖物对象的`remove()`方法,将其从地图中移除。 以下是一个示例代码,演示了如何清除指定覆盖物。 ```javascript // 创建地图实例 var map = new BMap.Map("map-container"); // 添加覆盖物,并为其设置唯一标识符 var marker1 = new BMap.Marker(new BMap.Point(116.404, 39.915)); marker1.myId = "marker1"; map.addOverlay(marker1); var marker2 = new BMap.Marker(new BMap.Point(116.417, 39.909)); marker2.myId = "marker2"; map.addOverlay(marker2); // 清除指定覆盖物 function removeOverlayById(id) { var overlays = map.getOverlays(); for (var i = 0; i < overlays.length; i++) { if (overlays[i].myId === id) { map.removeOverlay(overlays[i]); break; } } } // 调用清除覆盖物的方法 removeOverlayById("marker1"); ``` 以上代码示例中,我们通过为每个覆盖物对象设置了`myId`属性,代表它们的唯一标识符。然后,通过遍历地图上所有覆盖物对象的方式,找到需要清除覆盖物对象,并调用地图实例的`removeOverlay()`方法进行清除。 希望以上回答对您有帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值