Baidu map JavaScript How to clear the specified type of overlay

Source: Internet
Author: User

Because there are many types of overlays in a map, because of an overlay (which is generally custom), only one occurrence is allowed throughout the map

The first thing to think about is that each time you create this type of overlay, clear this type of covering, a simple way to judge the type of cover instanceof, part of the code is as follows (red code is the focus):

Poimarker.prototype =NewBmap.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 Click eventDiv.onclick = (e) = = {                            //Suppress event bubblingConst Oevent = e | |event; Oevent.cancelbubble=true; //Delete other Poiinfowindowmarker                            //get all the overlays on the map                            Const Alloverlay = This._map.getoverlays ();                                    For (const item of Alloverlay) {if (item instanceof Poiinfowindowmarker) { this . _map.removeoverlay (item); }} Const Poiinfowindowmarker=NewPoiinfowindowmarker ( This. _point, This. _img); //Add a node number overlay to the map (and register the overlay)                             This. _map.addoverlay (Poiinfowindowmarker);                        };                        Map.getpanes (). Labelpane.appendchild (Div); returnDiv;                    }; 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

Comes with a GIF plot:

Baidu Map gives the content of the label according to the comparison after deleting the overlay (not quite fit my needs of the scene)

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 = = "I am id=1") {                map.removeoverlay (alloverlay[i]);                 return false ;            }        }    }

Baidu map JavaScript How to clear the specified type of overlay

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.