How to use MapFunctionality to change layer visibility


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

The SetVisibleLayers() method enables a developer to change the visibility of individual layers in a map resource. The GetVisibleLayers() method (not used in the sample) uses a layer id to determine if a layer is visible. To change the visible layers for a resource in a map, use the SetVisibleLayers method. Pass the layer id and a boolean to set visibility. The sample below 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, simply refreshing the Toc will work. If a callback or partial postback (e.g. 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;
    queryFunctionality.GetQueryableLayers(null, out layerIDs, out 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
    queryFunctionality.GetQueryableLayers(Nothing, 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 sample assumes a Map (named Map1) and possibly a TOC (named Toc1) are in scope.