With OpenLayers, what is the correct way of removing a markers layer, and the popups?

24,437

Solution 1

You can remove individual markers from a marker layer with:

markers.removeMarker(marker);

Removing the entire layer, with markers should be achieved with:

markers.destroy();

You should be able to remove a popup with:

map.removePopup(popup);

where popup is the Popup object created earlier.

Solution 2

I know this post is old but to remove all markers from the marker layer list use:

markerLayer.clearMarkers();
Share:
24,437
GilShalit
Author by

GilShalit

Currently owner and principal at DHDev where we do Digital Humanities development. Previously - Founder and VP R&D at InnovizeIT, Leaders in DB2 application analysis and optimization (Mainframe and Open Systems)

Updated on July 05, 2022

Comments

  • GilShalit
    GilShalit almost 2 years

    LoadPin is a function to add a marker to a map. It initializes the layer on the first call. map is an openlayers map object.

    But using map.removeLayer("markers") or "Markers", does not remove the markers from the map. I saw a mention of a destroy operation to do this but cant find that.

    AND, how do I remove the popups?

    var markers = null
    function LoadPin(LL, name, description) {
        var size = new OpenLayers.Size(36, 47);
        var offset = new OpenLayers.Pixel(-(size.w / 2), -size.h);
        var icon = new OpenLayers.Icon('http://www.waze.co.il/images/home.png', size, offset);
    
        if (markers == null) {
            markers = new OpenLayers.Layer.Markers("Markers");
            map.addLayer(markers);
        }
    
        var marker = new OpenLayers.Marker(LL, icon)
        markers.addMarker(marker);
        var bounds = markers.getDataExtent();
        map.zoomToExtent(bounds);
    
        map.addPopup(new OpenLayers.Popup.FramedCloud("test", LL, null,
                    "<div style='font-family:Arial,sans-serif;font-size:0.8em;'>" + name + "<br>" + description + "</div>",
                    anchor = null, true, null));
    }
    
  • GilShalit
    GilShalit over 13 years
    Worked like a charm! Thanks!
  • Hayden Thring
    Hayden Thring over 9 years
    I like this method, but unsure the relevance of docs desc: "This method removes all markers from a layer. The markers are not destroyed by this function, but are removed from the list of markers."
  • Christophe Roussy
    Christophe Roussy over 9 years
    @HaydenThring If there is no other reference to them they will be garbage collected.