AnsweredAssumed Answered

ArcGIS remote map service - cannot set sublayer visibility property

Question asked by geonetadmin on Sep 13, 2012
Latest reply on Sep 20, 2012 by geonetadmin
Original User: theskyishard

Hi all,

I am trying to connect to a remote ArcGIS map service (not using REST) and provide the ability for users to toggle the visibility of the sub layers defined within the retrieved map. I am working in C# and an integrating ArcGIS 10.1 into a stand-alone application.

I am able to pull down the main map and store a list of the sublayers in order to keep track of names and visibility (I am only interested in going one step deep, I am not worried about any sublayers beneath the first set), but when I attempt to set the visibility property on a sublayer, the property appears to immeadiately change itself back to the original value... which is confusing me slightly (a lot!).

This is the offending block of code, see the example app for more detail:

        /// <summary>         /// Loads the layer from the remote map server info contained in the MapLayer object         /// </summary>         /// <param name="layer">MapLayer to load</param>         public void LoadLayerFromRemoteServer(MapLayer layer)         {             AGSServerConnectionFactory AGSConnectionFactory = new AGSServerConnectionFactoryClass();              IPropertySet2 propertySet = new PropertySetClass();             propertySet.SetProperty("URL", layer.HostPath);              IAGSServerConnection AGSServerConnection = AGSConnectionFactory.Open(propertySet, 0);              var serverObjectNames = AGSServerConnection.ServerObjectNames;             var objectName = serverObjectNames.Next();              while (objectName != null)             {                 if ((objectName.Name == layer.ServiceName) && (objectName.Type == "MapServer"))                 {                     // Found our map service!                     break;                 }                  objectName = serverObjectNames.Next();             }              IName name = (IName)objectName;             var mapServer = (IMapServer)name.Open();              // Connect to the map service             IMapServerLayer mapServerLayer = new MapServerLayerClass();             mapServerLayer.ServerConnect(objectName, mapServer.DefaultMapName);              ICompositeLayer2 compositeLayer = mapServerLayer as ICompositeLayer2;              if (layer.MapSubLayers.Count > 0)             {                 for (int subLayerIndex = 0; subLayerIndex < compositeLayer.Count; subLayerIndex++)                 {                     ILayer2 mapSubLayer = compositeLayer.get_Layer(subLayerIndex) as ILayer2;                      foreach (var subLayer in layer.MapSubLayers)                     {                         if (string.Compare(subLayer.Name, mapSubLayer.Name, StringComparison.InvariantCultureIgnoreCase) == 0)                         {                             Console.WriteLine("Layer visible before change: {0}", mapSubLayer.Visible);                              // This is the line that fails to behave as expected!!!                             ((ILayer)mapSubLayer).Visible = subLayer.IsDisplayed;                              Console.WriteLine("Layer visible after change: {0}", mapSubLayer.Visible);                              Console.ReadLine();                         }                     }                 }             }         }

I have tried a few different ways of setting the visibility by casting the layers into different types now and have trawled google and the forums without any success, but being fairly new to working with ArcGIS (this being my crash course on the subject) I am hoping that I am just missing something trivial.

I have written a small console app to demonstrate the problem, which contains all the relevant logic from the application I am trying to integrate. You shouldn't have to do anything more than provide a couple of referenced ESRI DLLs to build it, but let me know if you have any issues.

Can anyone provide any insights into what could be causing the issue and possible solutions?