Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing a layer in OpenLayers

I don't know why this isn't working. I would assume the answer is really simple. I need to remove a layer before adding a new one.

                     if (graphic) {
        window.map.removeLayer(graphic);
    }
    var graphic = new OpenLayers.Layer.Image(
        'Sightline'+''+SC,
        url,
        new OpenLayers.Bounds(derWesten[0].firstChild.nodeValue,derSueden[0].firstChild.nodeValue,derOsten[0].firstChild.nodeValue, derNorden[0].firstChild.nodeValue),
        new OpenLayers.Size(0,0),
        options
    );

    window.map.addLayer(graphic);   

It just keeps piling on the layers and not removing any. Any help?

like image 716
Mitchell Ingram Avatar asked Aug 25 '12 22:08

Mitchell Ingram


People also ask

How do you remove layers from Openlayers?

forEach(layer => map. removeLayer(layer)); Alternatively, you can also use the layer collection's internal forEach() method for similar results.


1 Answers

Use map.getLayersByName(layerName) get the layers. You might need to keep track of the names of the layers in some array or something

The method returns an array, so you go through the array of layers and use map.removeLayer(layer).

You can externalize this solution in a different function if you want and it works.

like image 187
M-k Avatar answered Oct 05 '22 23:10

M-k