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

Andreas Hocevar ahocevar at opengeo.org
Thu Jul 2 10:58:27 EDT 2009


Paul james wrote:
> Hi Andreas...
> I tried your code, but it never execute method initialize...
> Am I supposed to call that manually? And how about the parameters?

No, I made the snippet without testing, and you have to override
Layer.WMS if you want a custom initialize method to be executed.

So instead of

var _layer = new OpenLayers.Layer.WMS(...);

you could try

var CustomLayer = OpenLayers.Class(OpenLayers.Layer.WMS, {
    initialize: function(...){...},
    setLayerVisibility: function(...){...}
});

var _layer = new CustomLayer(
    /* everything except the object with initialize and
setLayerVisibility */
);

Regards,
Andreas.

>
> Thanks
>
>
> On Wed, Jul 1, 2009 at 6:12 PM, Andreas Hocevar <ahocevar at opengeo.org
> <mailto:ahocevar at opengeo.org>> wrote:
>
>     Oops, little mistake. My snippet should read
>
>     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});
>           }
>       }
>     );
>
>     On Wed, Jul 1, 2009 at 11:11 PM, Andreas
>     Hocevar<ahocevar at opengeo.org <mailto:ahocevar at opengeo.org>> wrote:
>     > Hi,
>     >
>     > On Wed, Jul 1, 2009 at 9:08 PM, Paul james<pauljame at gmail.com
>     <mailto: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 <mailto:Users at openlayers.org>
>     >> http://openlayers.org/mailman/listinfo/users
>     >>
>     >>
>     >
>     >
>     >
>     > --
>     > Andreas Hocevar
>     > OpenGeo - http://opengeo.org/
>     > Expert service straight from the developers.
>     >
>
>
>
>     --
>     Andreas Hocevar
>     OpenGeo - http://opengeo.org/
>     Expert service straight from the developers.
>
>


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




More information about the Users mailing list