[OpenLayers-Users] Mutually exclusive layers

Alexandre Dube adube at mapgears.com
Mon Feb 2 09:30:59 EST 2009


Hi Derek,

Derek Watling wrote:
> On my map I have 3 overlay layers, 2 of which need to be mutually exclusive,
> ie. can't both be on at the same time.
>
> I have be playing with the layer events and tried both "visibilitychanged"
> and "loadstart". I am getting the same result with both of these in that the
> event is triggered for each layer I have registered it on when the page
> first loads, but when switching the layers on/off in the LayerSwitcher the
> event isn't triggered.
>
> My code looks like:
>
> function init() {
> ...
>     streetNumbers = new OpenLayers.Layer.WMS("Street Numbers",
> "http://dev/Tilecache/tilecache.py?", { layers: "StreetNumbers", format:
> "image/png"});
>     streetNumbers.setZIndex(0);
>     streetNumbers.setVisibility(false);
>     streetNumbers.setIsBaseLayer(false);
>     streetNumbers.displayOutsideMaxExtent = 0;
>     streetNumbers.transitionEffect = "resize";
>     map.addLayer(streetNumbers);
>
>     erfNumbers = new OpenLayers.Layer.WMS("Erf Numbers",
> "http://dev/Tilecache/tilecache.py?", {layers: "ErfNumbers", format:
> "image/png"});
>     erfNumbers.setZIndex(0);
>     erfNumbers.setVisibility(false);
>     erfNumbers.setIsBaseLayer(false);
>     erfNumbers.displayOutsideMaxExtent = 0;
>     erfNumbers.transitionEffect = "resize";
>     map.addLayer(erfNumbers);
>     
>     zoneLabel = new OpenLayers.Layer.WMS("Zone",
> "http://dev/Tilecache/tilecache.py?", {layers: "Zone", format:
> "image/png"});
>     zoneLabel.setZIndex(0);
>     zoneLabel.setVisibility(false);
>     zoneLabel.setIsBaseLayer(false);
>     zoneLabel.displayOutsideMaxExtent = 0;
>     zoneLabel.transitionEffect = "resize";
>     map.addLayer(zoneLabel);
>
>     streetNumbers.events.register('loadstart', null,
> LayerChanged(streetNumbers));
>     erfNumbers.events.register('loadstart', null, LayerChanged(erfNumbers));
>   

Try this instead :

erfNumbers.events.register('visibilitychanged', this, LayerChanged);

'this' is the current object, i.e. erfNumbers.

> ...
> }
>
> function LayerChanged(Layer) {
>     alert(Layer.name);
> ...
> }
>
>   

Hope this helps,

-- 
Alexandre Dubé
Mapgears
www.mapgears.com




More information about the Users mailing list