[OpenLayers-Users] Re: removing a layer

Puneet Kishor punk.kish at gmail.com
Thu Jan 19 17:19:47 EST 2012


I am completely at my wits end. I am trying to check if a vector layer exists, and if it does, I need to either completely remove it or remove all the features from it (whichever is more efficient from the point of speed and memory), and then redraw that layer. Have been trying all sorts of permutations and combinations, but am just not successful.

My latest code attempt is below, and the original attempt is below that from my OP.

    var arr = map.getLayersByName("Vector layer");
    
    if (arr.length) {
        //map.removeLayer(arr[0]);
        layer.removeAllFeatures();
    }
    else {
        layer = new OpenLayers.Layer.Vector("Vector layer", {styleMap: myStyles});
    }

Neither `map.removeLayer(arr[0])` nor `layer.removeAllFeatures()` works.

Would appreciate suggestions.

On Jan 19, 2012, at 3:00 PM, Puneet Kishor wrote:

> I am trying to remove a layer with
> 
>    if (map.getLayerIndex(layer) > 0) {
>        map.removeLayer(layer);
>    }
> 
> but no joy. How can I remove a layer?
> 
> A bonus question: if I want to reconstruct a  vector layer on map "zoomend", that is, redraw all the features on it so they are resized accordingly, should I remove the layer (hence, the question above), or simply remove all the features and redraw the features? If the latter, then how?
> 
> Many thanks,
> 
> --
> Puneet Kishor




More information about the Users mailing list