-1

How to remove added WMS layer from OpenLayers? I have seen a link here, but I didn't get clearly.

I have added the WMS layer as follows:

<script src="../OpenLayers.js"></script>
<script type="text/javascript">
    var map, layer;
    function init(){
        map = new OpenLayers.Map( 'map' );
        layer = new OpenLayers.Layer.WMS( "OpenLayers WMS",
                "http://localhost:8090/geoserver/worksapce/wms", {layers: 'basic'} );
        var layer_1=new OpenLayers.Layer.WMS( "OpenLayers WMS",
                "http://localhost:8090/geoserver/worksapce/wms", 
                 {
                 layers:"states",
                 format:'image/png'
                 },
                 {isBaseLayer:false}
                 );
        map.addLayer([layer,layer_1);
        map.addControl( new OpenLayers.Control.LayerSwitcher() );
    }
</script>

This code is working fine and adding layer_1 as a overlay layer. Similarly I added 5 more over layers as above, now I want to remove layer_1 using OpenLayers code like map.removeLayer(layer_1);.

Please help me

  • 1
    Could you be a bit more specific about what you are trying to do. – John Powell Dec 17 '14 at 12:12
1

I guess your problem is one of Javascrip variable scope.

The variable layer_1 is only visible in the init() function, so you cannot access it outside of that function.

A solution is to declare it global, that is add to the line

var map, layer

so this reads

var map, layer, layer_1; // and layer_2,...

and then use the global variable in the init function (that is, edit var layer_1 = ... to read layer_1 = ...).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.