How to use MapFunctionality to change layer visibility


Summary
This topic shows how to use MapFunctionality to change the visibility of layers associated with a resource.

Using MapFunctionality to change layer visibility

The SetVisibleLayers() method enables a developer to change the visibility of individual layers in a map resource. The GetVisibleLayers() method (not used in the topic) uses a layer ID to determine if a layer is visible.
Do the following:
  1. To change the visible layers for a resource in a map, use the SetVisibleLayers method.
  2. Pass the layer ID and a Boolean to set visibility. The following code example sets the visibility for all layers to false, sets the visibility of the first layer to true, then refreshes the Map and Toc, if available. If a full page postback, refresh the Toc. If a callback or partial postback (for example, in a tool implementation), refresh the Toc and append the Toc callback results to the Maps callback result collection (via the CallbackResults property).
[C#]
ESRI.ArcGIS.ADF.Web.DataSources.IGISResource gisResource = mapFunctionality.Resource;
bool supported = gisResource.SupportsFunctionality(typeof
    (ESRI.ArcGIS.ADF.Web.DataSources.IQueryFunctionality));

if (supported)
{
    ESRI.ArcGIS.ADF.Web.DataSources.IQueryFunctionality queryFunctionality = 
        (ESRI.ArcGIS.ADF.Web.DataSources.IQueryFunctionality)
        gisResource.CreateFunctionality(typeof
        (ESRI.ArcGIS.ADF.Web.DataSources.IQueryFunctionality), null);

    string[] layerIDs = null;
    string[] layerNames = null;

    //Get the only queryable layers. 
    queryFunctionality.GetQueryableLayers(null, out layerIDs, out layerNames);

    //Note, it is possible all layers in the map are not queryable and hence 
    //their visibility is not toggled. To fetch all layers use:
    // mapFunctionality.GetLayers(layerIDs, layerNames)  


    // Turn off all layers.
    for (int i = 0; i < layerIDs.Length; i++)
    {
        mapFunctionality.SetLayerVisibility(layerIDs[i], false);
    }

    // Turn on the first layer.
    mapFunctionality.SetLayerVisibility(layerIDs[0], true);

    // Refresh resource display in the map.
    Map1.RefreshResource(gisResource.Name);

    // Refresh Toc to reflect visibility changes.
    Toc1.Refresh();

    // If Toc available, access a legend's symbol swatch.
    ESRI.ArcGIS.ADF.Web.DataSources.IMapTocFunctionality mapTocFunctionality = 
        (ESRI.ArcGIS.ADF.Web.DataSources.IMapTocFunctionality)Toc1.GetFunctionality
        (0);

    bool useMimeData = false;
    bool showAllDataFrames = false;

    ESRI.ArcGIS.ADF.Web.TocDataFrame[] tocDataFrameArray =
        mapTocFunctionality.GetMapContents(mapFunctionality.Name,
        ESRI.ArcGIS.ADF.Web.WebImageFormat.JPG, useMimeData, showAllDataFrames);

    foreach (ESRI.ArcGIS.ADF.Web.TocLayer tocLayer in tocDataFrameArray[0])
    {
        for (int index = 0; index < tocLayer.TocSymbolGroupCount; index++)
        {
            TocSymbolGroup tocSymbolGroup = tocLayer.GetTocSymbolGroup(index);
            foreach (TocSymbol tocSymbol in tocSymbolGroup)
            {
                CartoImage swatchCartoImage = tocSymbol.Image;
                System.Drawing.Bitmap swatchBitmap = swatchCartoImage.GetImage();
            }
        }
    }
}
[VB.NET]
Dim gisResource As ESRI.ArcGIS.ADF.Web.DataSources.IGISResource = mapFunctionality.Resource
Dim supported As Boolean = gisResource.SupportsFunctionality(GetType(ESRI.ArcGIS.ADF.Web.DataSources.IQueryFunctionality))

If supported Then
    Dim queryFunctionality As ESRI.ArcGIS.ADF.Web.DataSources.IQueryFunctionality = CType(gisResource.CreateFunctionality (GetType(ESRI.ArcGIS.ADF.Web.DataSources.IQueryFunctionality), Nothing), ESRI.ArcGIS.ADF.Web.DataSources.IQueryFunctionality)
    
    Dim layerIDs As String() = Nothing
    Dim layerNames As String() = Nothing
    
    'Get the only queryable layers.
    queryFunctionality.GetQueryableLayers(Nothing, layerIDs, layerNames)
    
    / / Note, it Is possible all layers in the map are Not queryable And hence
 / / their visibility Is Not toggled. To fetch all layers use:
    / / mapFunctionality.GetLayers(layerIDs, layerNames)
    
    ' Turn off all layers.
    Dim i As Integer = 0
    Do While i < layerIDs.Length
        mapFunctionality.SetLayerVisibility(layerIDs(i), False)
        i + = 1
    Loop
    
    ' Turn on the first layer.
    mapFunctionality.SetLayerVisibility(layerIDs(0), True)
    
    ' Refresh resource display in the map.
    Map1.RefreshResource(gisResource.Name)
    
    ' Refresh Toc to reflect visibility changes.
    Toc1.Refresh();
    
    ' If Toc available, access a legend's symbol swatch.
    Dim mapTocFunctionality As ESRI.ArcGIS.ADF.Web.DataSources.IMapTocFunctionality = CType(Toc1.GetFunctionality(0), ESRI.ArcGIS.ADF.Web.DataSources.IMapTocFunctionality)
    
    Dim useMimeData As Boolean = False
    Dim showAllDataFrames As Boolean = False
    
    Dim tocDataFrameArray As ESRI.ArcGIS.ADF.Web.TocDataFrame() = mapTocFunctionality.GetMapContents(mapFunctionality.Name, ESRI.ArcGIS.ADF.Web.WebImageFormat.JPG, useMimeData, showAllDataFrames)
    
    For Each tocLayer As ESRI.ArcGIS.ADF.Web.TocLayer In tocDataFrameArray(0)
        Dim index As Integer = 0
        Do While index < tocLayer.TocSymbolGroupCount
            Dim tocSymbolGroup As TocSymbolGroup = tocLayer.GetTocSymbolGroup(index)
            
            For Each tocSymbol As TocSymbol In tocSymbolGroup
                Dim swatchCartoImage As CartoImage = tocSymbol.Image
                Dim swatchBitmap As System.Drawing.Bitmap = swatchCartoImage.GetImage()
            Next tocSymbol
            index + = 1
        Loop
    Next tocLayer
End If






Additional Requirements
  • The topic assumes a Map (named Map1) and possibly a TOC (named Toc1) are in scope.