Infrastructure Map Server Forum
Welcome to Autodesk’s Infrastructure Map Server Forums. Share your knowledge, ask questions, and explore popular Infrastructure Map Server topics.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

switching layers on and off

7 REPLIES 7
Reply
Message 1 of 8
Sonja
334 Views, 7 Replies

switching layers on and off

Hi,

I need to know how to do the following in javascript (webpage) :
A mwf contains for example 4 layers and if the user switch on one layer from the legend it displays on the map, if he selects another layer from the legend the first layer must switch off and only the second layer must show on the map. I want to do this in the onViewChanged function in javascript?

Thanks,
Sonja
7 REPLIES 7
Message 2 of 8
Anonymous
in reply to: Sonja

Yes, I would use the onViewChanged event and create
a toggle statement something like:

 

  var
Layer1=map.getMapLayer("Layer-A");
  var
Layer2=map.getMapLayer("Layer-B");
  if (Layer1.getVisibility()) 
//is true
       {

       
 Layer2.setVisibility(true);

      
}
 else if (Layer2.getVisibility())  //is true
  
    {

     
Layer1.setVisibility(false);

       }
Good
luck

ZB

 

 



style="PADDING-RIGHT: 0px; PADDING-LEFT: 5px; MARGIN-LEFT: 5px; BORDER-LEFT: #000000 2px solid; MARGIN-RIGHT: 0px">Hi,

I need to know how to do the following in javascript (webpage) :
A mwf
contains for example 4 layers and if the user switch on one layer from the
legend it displays on the map, if he selects another layer from the legend the
first layer must switch off and only the second layer must show on the map. I
want to do this in the onViewChanged function in javascript?

Thanks,
Sonja

Message 3 of 8
Anonymous
in reply to: Sonja

sorry, just noticed mistake

 

should be:


  var
Layer1=map.getMapLayer("Layer-A");
  var
Layer2=map.getMapLayer("Layer-B");
  if (Layer1.getVisibility()) 
//is true
       {

       
 Layer2.setVisibility(false);

      
}
 else if (Layer2.getVisibility())  //is true
  
    {

     
Layer1.setVisibility(false);

      
}


style="PADDING-RIGHT: 0px; PADDING-LEFT: 5px; MARGIN-LEFT: 5px; BORDER-LEFT: #000000 2px solid; MARGIN-RIGHT: 0px">

Yes, I would use the onViewChanged event and
create a toggle statement something like:

 

  var
Layer1=map.getMapLayer("Layer-A");
  var
Layer2=map.getMapLayer("Layer-B");
  if (Layer1.getVisibility()) 
//is true
       {

       
 Layer2.setVisibility(true);

      
}
 else if (Layer2.getVisibility())  //is true
  
    {

     
Layer1.setVisibility(false);

       }
Good
luck

ZB

 

 



style="PADDING-RIGHT: 0px; PADDING-LEFT: 5px; MARGIN-LEFT: 5px; BORDER-LEFT: #000000 2px solid; MARGIN-RIGHT: 0px">Hi,

I need to know how to do the following in javascript (webpage) :
A
mwf contains for example 4 layers and if the user switch on one layer from
the legend it displays on the map, if he selects another layer from the
legend the first layer must switch off and only the second layer must show
on the map. I want to do this in the onViewChanged function in javascript?

Thanks,
Sonja

Message 4 of 8
Sonja
in reply to: Sonja

Thanks for the reply. I just have another problem. If I have only 2 layers on the map this code will work but what if I have more than 2 layers. I don't exactly know what layer the user selects because the code check if a layer is on or off so if the user select a layer and the other layer is on, the program will validate the layers and see that 2 layers is on so which on do you switch off and which one stays on.

Maybe there is a onclick event of a layer to get the selected layer but I'm not sure if that is possible.

Thanks in advance,
Sonja
Message 5 of 8
Anonymous
in reply to: Sonja

Sonja,

since you only need one layer turned on at the time, besides some background
layers possibly, you can use a global variable to store the current
"on"-layer.
The workflow could be something like this:

>user starts app
onMapLoaded store the initial layerName in a global variable

> user does something on the map (possibly turns another layer on)
onViewChanged loop over all layers
check for visibility
if visible
check layerName against your global variable
if true (old layer still visible)
set temp variable 1 using layerName
if false (new layer or background layer)
check if layer is part of backgroundlayers
if false (new layer visble)
set temp variable 2 using layerName
if both temp variables contain values (we've got a winner)
turn old layer off
update global variable with new layerName
if not (damage control)
if temp variable 1 is empty (old layer not visible)
user has switched the one layer off, how rude
do something to correct this
if temp variable 2 is empty (no new layer visible)
do nothing, user hasn't touched the layers in the legend

It's a bit of coding but it should work OK

Hope this helps,

Johan van Vliet
The Netherlands
Message 6 of 8
Sonja
in reply to: Sonja

Thank you very much, it worked.
Message 7 of 8
Anonymous
in reply to: Sonja

Hi,
messageID=891254..

I too want the same requirement, but i have given the code as below in onViewChanged

var map_1 = getMap();
var pending_move_layer = map_1.getMapLayer("Pending Move");
var space_label_layer = map_1.getMapLayer("Space Label");

if(pending_move_layer == null)
alert ("Pending Vacant Layer not found");
else
{
space_label_layer.setVisibility(!pending_move_layer.getVisibility());
}
}

This works fine if the map is loaded first time but does not work when i turn on or off some time later.
Message 8 of 8
bjeffers
in reply to: Sonja

This is a cool tool. Where would you put this code?

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report