ArcGIS API for Silverlight - Library Reference
GetLayerVisibility Method
See Also  Example Send comments on this topic
ESRI.ArcGIS.Client Namespace > ArcGISTiledMapServiceLayer Class : GetLayerVisibility Method

layerID
The sub-layer ID.
Gets the sub-layer visibility.

Syntax

Visual Basic (Declaration) 
Public Function GetLayerVisibility( _
   ByVal layerID As Integer _
) As Boolean
C# 
public bool GetLayerVisibility( 
   int layerID
)

Remarks

The GetLayerVisibility Method returns a boolean for a specific sub-layer ID of an ArcGISTiledMapServiceLayer telling whether it is visible (true) or not (false) in the web service.

It is not possible to control the sub-layer visibility of an ArcGISTiledMapServiceLayer. This is because the cached tiles are pre-constructed on ArcGIS Server. These cached image tiles cannot be regenerated on-the-fly. If you need to show those sub-layers from an ArcGISTiledMapServiceLayer that were set to be invisible by the map service, what you can do is create an ArcGISDynamicMapServiceLayer using the URL of the ArcGISTiledMapServiceLayer. Then adjust the sub-layer visibility in the ArcGISDynamicMapServiceLayer to show those sub-layer that were defined as invisible in the map service.

Another option for determining if the sub-layer of an ArcGISTiledMapServiceLayer is visible or not is to enter the Url for the sub-layer in the address bar of an internet browser and look for the Default Visibility section in the ArcGIS REST Services Directory. Remember the sub-layer has the same Url as the main ArcGISTiledMapServiceLayer but with a forward slash and the sub-layer ID value appended to the Url (ex: http://server.arcgisonline.com/ArcGIS/rest/services/Reference/ESRI_BoundariesPlaces_World_2D/MapServer/0). See the following screen shot:

Determining the sub-layer visibility using the ArcGIS REST Services Directory.

Parameters

layerID
The sub-layer ID.

Return Value

The sub-layer visibility

Example

How to use:

When the application loads an ArcGISTiledMapServiceLayer will automatically be added to the Map Contol (Map1). The ID of each sub-layer will be displayed as a CheckBox. If the CheckBox is 'checked' that means the 'Default Visibility' = True for the ArcGISTiledMapServiceLayer. Click the Button to generate a new ArcGISDynamicMapServiceLayer based upon the URL of the original ArcGISTiledMapServiceLayer. Put the ArcGISDynamicMapServiceLayer in a Map Control (Map2). The Initialized Event of the ArcGISDynamicMapServiceLayer will have code that only displays those sub-layers in the ArcGISTiledMapServiceLayer that had a 'Default Visibility' = False (i.e. those with CheckBoxes that were unchecked.

The XAML code in this example is used in conjunction with the code-behind (C# or VB.NET) to demonstrate the functionality.

The following screen shot corresponds to the code example in this page.

Using the ArcGISTiledMapServiceLayer.GetLayerVisibility Property to create an ArcGISDynamicMapServiceLayer.

XAMLCopy Code
<Grid x:Name="LayoutRoot" >
  
  <!-- 
  Add a Map Control and some basic instructions (the Label). Set the Extent to Central and South America. 
  -->
  <sdk:Label Height="71" HorizontalAlignment="Left" Margin="0,168,0,0" Name="Label2" 
       VerticalAlignment="Top" Width="205" >
    <TextBlock Text="The original ArcGISTiledMapServiceLayer with Default Visibility of sub-layers (shown left):" 
               TextWrapping="Wrap" Height="78" Width="188" />
  </sdk:Label>
  <esri:Map x:Name="Map1" WrapAround="True" HorizontalAlignment="Left" VerticalAlignment="Top" 
        Margin="0,238,0,0" Height="350" Width="205" Extent="-10223280,-1227779,-6845528,2924383">
    <!-- 
    Add an ArcGISTiledMapServiceLayer. Wire-up the Initilized Event to display a CheckBox for 
    each sub-layer's 'Default Visibility' returned from the map service. -->
    <esri:ArcGISTiledMapServiceLayer ID="World_Topo_Map"
          Url="http://server.arcgisonline.com/ArcGIS/rest/services/World_Topo_Map/MapServer" 
          Initialized="ArcGISTiledMapServiceLayer_Initialized"/>
  </esri:Map>
  
  <!-- 
  Add a ListBox to display each sub-layer visibility. Embedded in each row of the ListBox will be a 
  CheckBox (one for each sub-layer). The user will not be able to interact with the CheckBoxes; they are
  for informational purposes only. 
  -->
  <sdk:Label Height="35" HorizontalAlignment="Left" Margin="211,204,0,0" Name="Label1" 
       VerticalAlignment="Top" Width="80" >
    <TextBlock Text="Sub-layer ID Visibility:" TextWrapping="Wrap" Height="35" Width="84" />
  </sdk:Label>
  <ListBox Height="350" HorizontalAlignment="Left" Margin="211,238,0,0" Name="ListBox1" 
           VerticalAlignment="Top" Width="80" />
  
  <!-- Nice GUI seperator to control the flow of the application look. -->
  <Rectangle Height="460" HorizontalAlignment="Left" Margin="319,132,0,0" Name="Rectangle1" 
             Stroke="Black" StrokeThickness="2" VerticalAlignment="Top" Width="1" />
  
  <!-- 
  Add a Button that will allow the user to add an ArcGISDynamicMapServiceLayer to another Map Control 
  that is based upon those sub-layers in the ArcGISTiledMapServiceLayer that had 
  'Default Visibility' = false in the map service. 
  -->
  <Button Name="Button1" Height="100" HorizontalAlignment="Left" Margin="367,129,0,0" 
          VerticalAlignment="Top" Width="205" Click="Button1_Click" >
    <TextBlock Text="Create a new ArcGISDynamicMapServiceLayer based upon the original ArcGISTiledMapServiceLayer.Url but showing just the sub-layers that had 'Default Visiblity=false'" 
               TextWrapping="Wrap" />
  </Button>
  <esri:Map Background="White" HorizontalAlignment="Left" Margin="365,238,0,0" Name="Map2" 
        VerticalAlignment="Top" WrapAround="True" Height="350" Width="205"
        Extent="-10223280,-1227779,-6845528,2924383"/>
  
  <!-- Provide the instructions on how to use the sample code. -->
  <TextBlock Height="126" HorizontalAlignment="Left" Name="TextBlock1" VerticalAlignment="Top" Width="642" 
             TextWrapping="Wrap" Text="When the application loads an ArcGISTiledMapServiceLayer 
             will automatically be added to the Map Contol (Map1). The ID of each sub-layer will be displayed as 
             a CheckBox. If the CheckBox is 'checked' that means the 'Default Visibility' = True for the 
             ArcGISTiledMapServiceLayer. Click the Button to generate a new ArcGISDynamicMapServiceLayer 
             based upon the URL of the original ArcGISTiledMapServiceLayer. Put the ArcGISDynamicMapServiceLayer 
             in a Map Control (Map2). The Initialized Event of the ArcGISDynamicMapServiceLayer will have code that 
             only displays those sub-layers in the ArcGISTiledMapServiceLayer that had a 'Default Visibility' = 
             False (i.e. those with CheckBoxes that were unchecked." />
   
</Grid>
C#Copy Code
// It is not possible to control the sub-layer visibility of an ArcGISTiledMapServiceLayer. This is because
// the cached tiles are pre-constructed on ArcGIS Server. These cached image tiles cannot be regenerated
// on-the-fly. If you need to show those sub-layers from an ArcGISTiledMapServiceLayer that were set to be
// invisible by the map service, what you can do is create an ArcGISDynamicMapServiceLayer using the URL
// of the ArcGISTiledMapServiceLayer. Then adjust the sub-layer visibility in the ArcGISDynamicMapServiceLayer
// to show those sub-layer that were defined as invisible in the map service.
            
private void ArcGISTiledMapServiceLayer_Initialized(object sender, System.EventArgs e)
{
  // When the ArcGISTiledMapServiceLayer Initializes get the visibility for each sub-layer. Add a
  // CheckBox for each sub-layer ID and set it to be checked if the sub-layer is visible as defined
  // by the map service.
  
  // Get the ArcGISTiledMapServiceLayer from the Map Control (Map1).
  ESRI.ArcGIS.Client.ArcGISTiledMapServiceLayer myArcGISTiledMapServiceLayer = null;
  myArcGISTiledMapServiceLayer = (ESRI.ArcGIS.Client.ArcGISTiledMapServiceLayer)(Map1.Layers["World_Topo_Map"]);
  
  // Get the count of the number of sub-layers in the ArcGISDynamicMapServiceLayer. 
  int numLayers = myArcGISTiledMapServiceLayer.Layers.Length;
  
  // Loop through each sub-layer.
  for (var i = 0; i < numLayers; i++)
  {
    // Get the sub-layer visibility.
    bool myLayerVisibility = myArcGISTiledMapServiceLayer.GetLayerVisibility(i);
    
    // Create a StackPanel to hold other Framework Element Controls.
    StackPanel myStackPanel = new StackPanel();
    myStackPanel.Orientation = Orientation.Horizontal;
    
    // Create a CheckBox to display if the sub-layer is visible or not.
    // Note the sub-layer ID is embedded as the .Tag for the CheckBox.
    CheckBox myCheckBox = new CheckBox();
    myCheckBox.Content = i.ToString();
    myCheckBox.IsChecked = myLayerVisibility;
    myCheckBox.Tag = i;
    myCheckBox.IsEnabled = false;
    myStackPanel.Children.Add(myCheckBox);
    
    // Add the StackPanel into the ListBox.
    ListBox1.Items.Add(myStackPanel);
  }
}
  
private void Button1_Click(object sender, System.Windows.RoutedEventArgs e)
{
  // Create a new ArcGISDynamicMapServiceLayer using the URL from the ArcGISTiledMapServiceLayer. The
  // ArcGISDynamicMapServiceLayer will display the sub-layer that were invisible in the ArcGISImageServiceLayer.
  
  // Get the ArcGISTiledMapServiceLayer from the Map Control (Map1).
  ESRI.ArcGIS.Client.ArcGISTiledMapServiceLayer myArcGISTiledMapServiceLayer = null;
  myArcGISTiledMapServiceLayer = (ESRI.ArcGIS.Client.ArcGISTiledMapServiceLayer)(Map1.Layers["World_Topo_Map"]);
  
  // Create a new ArcGISDynamicMapServiceLayer using the URL from the ArcGISTiledMapServiceLayer.
  ESRI.ArcGIS.Client.ArcGISDynamicMapServiceLayer myArcGISDynamicMapServiceLayer = new ESRI.ArcGIS.Client.ArcGISDynamicMapServiceLayer();
  myArcGISDynamicMapServiceLayer.ID = "HiddenLayer";
  myArcGISDynamicMapServiceLayer.Url = myArcGISTiledMapServiceLayer.Url;
  
  // Wire-up the Initialized Event of the ArcGISDynamicMapServiceLayer. 
  myArcGISDynamicMapServiceLayer.Initialized += ArcGISDynamicMapServiceLayer_Initialized;
  
  // Add the ArcGISDynamicMapServiceLayer to the Map Control (Map2).
  Map2.Layers.Add(myArcGISDynamicMapServiceLayer);
}
  
private void ArcGISDynamicMapServiceLayer_Initialized(object sender, EventArgs e)
{
  // In order to change the visibility of the sub-layers in the ArcGISDynamicMapServiceLayer, you
  // need to perform this action in the Intialized Event or anytime after the Initialized Event occurs.
  // It was not possible to do this action in the Button Click Event because we did not have an asynchronous
  // round trip from ArcGIS Server yet!
  
  // Get the ArcGISDynamicMapServiceLayer from the Map Control (Map2).
  ESRI.ArcGIS.Client.ArcGISDynamicMapServiceLayer myArcGISDynamicMapServiceLayer = new ESRI.ArcGIS.Client.ArcGISDynamicMapServiceLayer();
  myArcGISDynamicMapServiceLayer = (ESRI.ArcGIS.Client.ArcGISDynamicMapServiceLayer)(Map2.Layers["HiddenLayer"]);
  
  // Create a List to hold which sub-layers are visible. This will be used later to construct the array.
  List<int> myList = new List<int>();
  
  // Loop through each StackPanel in the ListBox.
  foreach (StackPanel myStackPanel in ListBox1.Items)
  {
    // Get the relevent information about the sub-layer visibility from the CheckBox.
    CheckBox myCheckBox = (CheckBox)(myStackPanel.Children[0]);
  
    // Add only those sub-layers that were considered invisible by the map service in the ArcGISDynamicMapServiceLayer
    // into the List.
    if (myCheckBox.IsChecked == false)
    {
      myList.Add(Convert.ToInt32(myCheckBox.Tag));
    }
  }
  
  // Create an Integer array from the List. It will hold only those sub-layer that are to be visible on the client.
  int[] myVisibleLayers = myList.ToArray();
  
  // Use the VisibleLayers Property to control which sub-layers are drawn.
  myArcGISDynamicMapServiceLayer.VisibleLayers = myVisibleLayers;
  
  // Refresh the ArcGISDynamicMapServiceLayer to show only those sub-layers we defined as being visible on the client.
  myArcGISDynamicMapServiceLayer.Refresh();
}
VB.NETCopy Code
' It is not possible to control the sub-layer visibility of an ArcGISTiledMapServiceLayer. This is because
' the cached tiles are pre-constructed on ArcGIS Server. These cached image tiles cannot be regenerated
' on-the-fly. If you need to show those sub-layers from an ArcGISTiledMapServiceLayer that were set to be
' invisible by the map service, what you can do is create an ArcGISDynamicMapServiceLayer using the URL
' of the ArcGISTiledMapServiceLayer. Then adjust the sub-layer visibility in the ArcGISDynamicMapServiceLayer
' to show those sub-layer that were defined as invisible in the map service.
            
Private Sub ArcGISTiledMapServiceLayer_Initialized(sender As System.Object, e As System.EventArgs)
  
  ' When the ArcGISTiledMapServiceLayer Initializes get the visibility for each sub-layer. Add a
  ' CheckBox for each sub-layer ID and set it to be checked if the sub-layer is visible as defined
  ' by the map service.
  
  ' Get the ArcGISTiledMapServiceLayer from the Map Control (Map1).
  Dim myArcGISTiledMapServiceLayer As ESRI.ArcGIS.Client.ArcGISTiledMapServiceLayer
  myArcGISTiledMapServiceLayer = CType(Map1.Layers.Item("World_Topo_Map"), ESRI.ArcGIS.Client.ArcGISTiledMapServiceLayer)
  
  ' Get the count of the number of sub-layers in the ArcGISDynamicMapServiceLayer. 
  Dim numLayers As Integer = myArcGISTiledMapServiceLayer.Layers.Length
  
  ' Loop through each sub-layer.
  For i = 0 To numLayers - 1
    
    ' Get the sub-layer visibility.
    Dim myLayerVisibility As Boolean = myArcGISTiledMapServiceLayer.GetLayerVisibility(i)
    
    ' Create a StackPanel to hold other Framework Element Controls.
    Dim myStackPanel As New StackPanel
    myStackPanel.Orientation = Orientation.Horizontal
    
    ' Create a CheckBox to display if the sub-layer is visible or not.
    ' Note the sub-layer ID is embedded as the .Tag for the CheckBox.
    Dim myCheckBox As New CheckBox
    myCheckBox.Content = i.ToString
    myCheckBox.IsChecked = myLayerVisibility
    myCheckBox.Tag = i
    myCheckBox.IsEnabled = False
    myStackPanel.Children.Add(myCheckBox)
    
    ' Add the StackPanel into the ListBox.
    ListBox1.Items.Add(myStackPanel)
    
  Next
  
End Sub
            
Private Sub Button1_Click(sender As System.Object, e As System.Windows.RoutedEventArgs)
  
  ' Create a new ArcGISDynamicMapServiceLayer using the URL from the ArcGISTiledMapServiceLayer. The
  ' ArcGISDynamicMapServiceLayer will display the sub-layer that were invisible in the ArcGISImageServiceLayer.
  
  ' Get the ArcGISTiledMapServiceLayer from the Map Control (Map1).
  Dim myArcGISTiledMapServiceLayer As ESRI.ArcGIS.Client.ArcGISTiledMapServiceLayer
  myArcGISTiledMapServiceLayer = CType(Map1.Layers.Item("World_Topo_Map"), ESRI.ArcGIS.Client.ArcGISTiledMapServiceLayer)
  
  ' Create a new ArcGISDynamicMapServiceLayer using the URL from the ArcGISTiledMapServiceLayer.
  Dim myArcGISDynamicMapServiceLayer As New ESRI.ArcGIS.Client.ArcGISDynamicMapServiceLayer
  myArcGISDynamicMapServiceLayer.ID = "HiddenLayer"
  myArcGISDynamicMapServiceLayer.Url = myArcGISTiledMapServiceLayer.Url
  
  ' Wire-up the Initialized Event of the ArcGISDynamicMapServiceLayer. 
  AddHandler myArcGISDynamicMapServiceLayer.Initialized, AddressOf ArcGISDynamicMapServiceLayer_Initialized
  
  ' Add the ArcGISDynamicMapServiceLayer to the Map Control (Map2).
  Map2.Layers.Add(myArcGISDynamicMapServiceLayer)
  
End Sub
            
Private Sub ArcGISDynamicMapServiceLayer_Initialized(sender As Object, e As EventArgs)
  
  ' In order to change the visibility of the sub-layers in the ArcGISDynamicMapServiceLayer, you
  ' need to perform this action in the Intialized Event or anytime after the Initialized Event occurs.
  ' It was not possible to do this action in the Button Click Event because we did not have an asynchronous
  ' round trip from ArcGIS Server yet!
  
  ' Get the ArcGISDynamicMapServiceLayer from the Map Control (Map2).
  Dim myArcGISDynamicMapServiceLayer As New ESRI.ArcGIS.Client.ArcGISDynamicMapServiceLayer
  myArcGISDynamicMapServiceLayer = CType(Map2.Layers.Item("HiddenLayer"), ESRI.ArcGIS.Client.ArcGISDynamicMapServiceLayer)
  
  ' Create a List to hold which sub-layers are visible. This will be used later to construct the array.
  Dim myList As New List(Of Integer)
  
  ' Loop through each StackPanel in the ListBox.
  For Each myStackPanel As StackPanel In ListBox1.Items
    
    ' Get the relevent information about the sub-layer visibility from the CheckBox.
    Dim myCheckBox As CheckBox = CType(myStackPanel.Children.Item(0), CheckBox)
    
    ' Add only those sub-layers that were considered invisible by the map service in the ArcGISDynamicMapServiceLayer
    ' into the List.
    If myCheckBox.IsChecked = False Then
      myList.Add(CInt(myCheckBox.Tag))
    End If
    
  Next
  
  ' Create an Integer array from the List. It will hold only those sub-layer that are to be visible on the client.
  Dim myVisibleLayers() As Integer = myList.ToArray
  
  ' Use the VisibleLayers Property to control which sub-layers are drawn.
  myArcGISDynamicMapServiceLayer.VisibleLayers = myVisibleLayers
  
  ' Refresh the ArcGISDynamicMapServiceLayer to show only those sub-layers we defined as being visible on the client.
  myArcGISDynamicMapServiceLayer.Refresh()
  
End Sub

Requirements

Target Platforms: Windows XP Professional, Windows Server 2003 family, Windows Vista, Windows Server 2008 family, Windows 7

See Also

© ESRI, Inc. All Rights Reserved.