[OpenLayers-Users] TURN On layers Individually...

Andreas Hocevar ahocevar at opengeo.org
Wed Jul 1 17:11:07 EDT 2009


Hi,

On Wed, Jul 1, 2009 at 9:08 PM, Paul james<pauljame at gmail.com> wrote:
> I have that layer on Openlayers :
>
>       var _layer = new OpenLayers.Layer.MapServer(
>        "PointLayer",
>        _cgi,
>        {
>            map: _mapfile,
>            singleTile: true,
>             layers: ['Layer1', 'Layer2', 'Layer3'],
>            transparent: "true",
>            format: "image/png"
>        },
>     );
>
> If I use setVisibility on PointLayer, Layer1, 2 and 3 are turned ON...
> How Can I turn on/off Layer1,Layer2,Layer3 individually on javascript?

Many ways to do that, no big deal. How 'bout

var _layer = new OpenLayers.Layer.WMS(
    "PointLayer",
    _cgi,
    {
        map: _mapfile,
        singleTile: true,
        layers: ['Layer1', 'Layer2', 'Layer3'],
        transparent: true,
        format: 'image/png',
        initialize: function(name, url, params, options) {
            this.layerMap = {};
            var layer;
            for(var i=0; i<params.layers.length; ++i) {
                layer = params.layers[i];
                this.layerMap[layer] = true;
            }
            OpenLayers.Layer.WMS.prototype.initialize.apply(this, arguments);
        },
        setLayerVisibility: function(layer, visibility) {
            this.layerMap[layer] = visibility;
            var layers = [];
            for(var l in this.layerMap) {
                this.layerMap[l] && layers.push(l);
            }
            this.mergeNewParams({"LAYERS": layers});
        }
    }
);

Now you can e.g. use _layer.setLayerVisibility('Layer1', false) to
turn off Layer1. This would probably also work with Layer.MapServer,
but I have never used that, and MapServer supports Layer.WMS as well.

Regards,
Andreas.

>
> Paul
>
> _______________________________________________
> Users mailing list
> Users at openlayers.org
> http://openlayers.org/mailman/listinfo/users
>
>



-- 
Andreas Hocevar
OpenGeo - http://opengeo.org/
Expert service straight from the developers.



More information about the Users mailing list