Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

On my map I have 3 overlay layers, all of which need to be mutually exclusives, ie. can't be on (checked) at the same time. I thought of using events to trigger the overlays as the following in my index.html main file:

function init(){ 
 var map, kml_1Layer, kml_2Layer, kml_3Layer; 
...
kml_1Layer.events.register("visibilitychanged", this, layerChanged);
kml_2Layer.events.register("visibilitychanged", this, layerChanged);
kml_3Layer.events.register("visibilitychanged", this, layerChanged); 
...
}

function layerChanged(layer) {
     if (layer.object.visibility) {
     switch (layer.object.name) {
        case "kml_1Layer":
              kml_2Layer.setVisibility(false);
              kml_3Layer.setVisibility(false);
              break;
        case "kml_2Layer":
              kml_1Layer.setVisibility(false);
              kml_3Layer.setVisibility(false);
              break;
        case "kml_3Layer":
              kml_1Layer.setVisibility(false);
              kml_2Layer.setVisibility(false);
              break;
     }
  }
}

You can see the web site here: http://genepoulin.net/geo/index.html Obviously the events are not triggered.

What am i missing here? Thanks.

UPDATED and Resolved...

Corrected code (i was wrong naming the switch cases):

function init(){ 
 var map, kml_1Layer, kml_2Layer, kml_3Layer; 
...
kml_1Layer.events.register("visibilitychanged", kml_1Layer, layerChanged);
kml_2Layer.events.register("visibilitychanged", kml_2Layer, layerChanged);
kml_3Layer.events.register("visibilitychanged", kml_3Layer, layerChanged); 
...
}

function layerChanged(layer) {
     if (layer.object.visibility) {
     switch (layer.object.name) {
        case "*given label of layer1*":
              kml_2Layer.setVisibility(false);
              kml_3Layer.setVisibility(false);
              break;
        case "*given label of layer2*":
              kml_1Layer.setVisibility(false);
              kml_3Layer.setVisibility(false);
              break;
        case "*given label of layer3*":
              kml_1Layer.setVisibility(false);
              kml_2Layer.setVisibility(false);
              break;
     }
  }
} 

You can see the result on my page.

Thanks all.

share|improve this question
    
doesn't the linked question answer you problem? –  Devdatta Tengshe May 15 '13 at 3:39
1  
It seems to be the same question with two answers being offered to the original one. I too would prefer more work done on the original rather than asking the same question again. Perhaps this question could be used as an answer there along the lines of "I've tried to do the same thing this way, also without success - perhaps what I've tried will help us and others finally resolve your question". –  PolyGeo May 15 '13 at 6:51
    
If you read carefully it is not the same question!! I want to know why the specified event for the overlays is not triggered. –  volvox May 15 '13 at 9:29
    
I'm really disappointed of the comments here - my question is related to an event not triggered at the overlay level. –  volvox May 15 '13 at 9:33
1  
volvox, I see your point but I am reluctant to re-open this question until you can edit it to a manageable size. We are not a code review site, but basically you are asking us to go over many pages of code to find a potentially subtle bug. It would be to your benefit to work on this until you can isolate the problem and present it in as simple and clear a form as possible--preferably with much less code--then edit your question to reflect that. The editing will automatically put your question into a list of candidates for re-opening. –  whuber May 15 '13 at 12:13
show 2 more comments

1 Answer

up vote 1 down vote accepted

Hope it helps, I could not test it since out of office. This should be easier to maintain.

var radioLayer = [ kml_1Layer, kml_2Layer, kml_3Layer ];
var layerChanged = function( evt )
{
  var rList = this;
  if ( !evt.object.getVisibility() )
    return; // layer has been hidden, do not need check others
  for ( i = 0; i < rList.length; i++ )
    if ( rList[i] != evt.object )
      rList[i].setVisibility( false );
}

for ( i = 0; i < radioLayer.length; i++ )
  radioLayer[i].events.register( 'visibilitychanged', radioLayer, layerChanged );
share|improve this answer
    
It works fine - ... many thanks ... much better code then mine and as you say easier to maintain –  volvox May 17 '13 at 3:30
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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