[GIS] LayerSwitcher() not working with Group Layers from Geoserver

geoserveropenlayers-2

i have used LayerSwitcher() before and it has worked fine but I am now trying to use it with the group layers in Geoserver and the map blanks out.

tiled = new OpenLayers.Layer.WMS(
                    "Geoserver layers - Tiled", "http://niles.iplantcollaborative.org:80/geoserver/wms",
                    {
                        LAYERS: 'Shapefile.py',
                        STYLES: '',
                        format: format,
                        tiled: !pureCoverage,
                        tilesOrigin : map.maxExtent.left + ',' + map.maxExtent.bottom
                    },
                    {
                        buffer: 0,
                        displayOutsideMaxExtent: true,
                        isBaseLayer: true,
                        displayInLayerSwitcher: true
                    } 
                );
var imagery = new OpenLayers.Layer.WMS(
                "Global Imagery",
                "http://maps.opengeo.org/geowebcache/service/wms",
                {layers: "bluemarble"}
            );
                map.addLayers([tiled, imagery]);

                // build up all controls
                map.addControl(new OpenLayers.Control.PanZoomBar({
                    position: new OpenLayers.Pixel(2, 15)
                }));

                map.addControl(new OpenLayers.Control.Navigation());
                map.addControl(new OpenLayers.Control.Scale($('scale')));
                map.addControl(new OpenLayers.Control.MousePosition({element: $('location')}));
                map.addControl(new OpenLayers.Control.LayerSwitcher());
                map.zoomToExtent(bounds);

The moment I comment out the LayerSwitcher(), the map is rendered fine.

Also, I tried adding a new WMS, imagery (in the code above), and if I try to do the following,

map.addLayers([imagery, tiled]);

I am only able to see a broken image. Can we not add a layer through code with group layers?

Where am I going wrong?

Best Answer

I do not completely understand your question, but when it comes to layer groups from Geoserver affecting the LayerSwitcher in OpenLayers: this is impossible, OpenLayers cannot tell the difference between a layer group wms and a single layer wms.

As for your problem with "one layer switching off when the other is turned on": This is because OpenLayers treats your wms layers as baselayers. Try to add isBaseLayer: false to the properties of one of your layers (and perhaps transparent: true), then they will show up at the same time

Related Question