Select to view content in your preferred language

Layer Definition

1349
3
07-20-2012 06:33 AM
BrianLeroux
Frequent Contributor
I am playing around with Layer Definitions for a ArcGISDynamicMapService. I have never done this before and I am stuck. When I run this nothing happens in my map. It seems like after it hits the  dynamicServiceLayer.LayerDefinitions.Add(lDef); line it just stops without warning. I places a messagebox right after this line and it never shows. Any ideas? Thanks..

string myLayer = "PoliciesSDE";
            ArcGISDynamicMapServiceLayer dynamicServiceLayer = MapApplication.Current.Map.Layers[myLayer] as ArcGISDynamicMapServiceLayer;            
            LayerDefinition lDef = new LayerDefinition();
            lDef.LayerID = 0;           
            lDef.Definition = "State = 'OREGON'";
            dynamicServiceLayer.LayerDefinitions.Add(lDef);
            dynamicServiceLayer.Refresh();
0 Kudos
3 Replies
BrianLeroux
Frequent Contributor
This does not work eaither but is based on the sample in the Silverlight API Reference.
string myLayer = "PoliciesSDE";
            ArcGISDynamicMapServiceLayer dynamicServiceLayer = MapApplication.Current.Map.Layers[myLayer] as ArcGISDynamicMapServiceLayer;            
            LayerDefinition lDef = new LayerDefinition();
            lDef.LayerID = 0;           
            lDef.Definition = "State = 'OREGON'";

            // Create an ObservableCollection and add the .Definition to it.
            System.Collections.ObjectModel.ObservableCollection<LayerDefinition> myObservableCollection2 =
               new System.Collections.ObjectModel.ObservableCollection<LayerDefinition>();
            myObservableCollection2.Add(lDef);
            MessageBox.Show("Collection made.");
            dynamicServiceLayer.LayerDefinitions = (myObservableCollection2);
            MessageBox.Show("Definition Applied.");
            dynamicServiceLayer.Refresh();
0 Kudos
BrianLeroux
Frequent Contributor
So I changed the following line to use the Layers index number and now it works fine.
ArcGISDynamicMapServiceLayer dynamicServiceLayer = MapApplication.Current.Map.Layers[7] as ArcGISDynamicMapServiceLayer


Unfortunately this will not work for me as users have the ability to move layers in the map. I will also not be able to use the layers ID either because when I add the layer to the map using the Builder App it adds a ID like b47f9704b58b4aa6b77c93cf9faaf8f2 which varies from map to map when adding the same layer. I want my tool to go after the same layer in various map applications so I will not be able to use the layer ID as they will be different in every one.

The layer I want to add a definition to will always have the same name but I can't figure out how to reference a layer by name. Has anyone been able to do this?
0 Kudos
BrianLeroux
Frequent Contributor
I managed to get this to work. Iterating through the layers was the only way I could come up with to find the layer I wanted based on Layer Name. If anyone has a better way please let me know.

             string strState = cmbState.SelectionBoxItem.ToString().ToUpper();
             string strlDef = "State = '" + strState + "'";
             foreach (Layer myLayer in MapApplication.Current.Map.Layers)
             {
                 strLayerName = MapApplication.GetLayerName(myLayer);

                 if (strLayerName == "PoliciesSDE")
                 {
                     string layerID = myLayer.ID;
                     LayerDefinition lDef = new LayerDefinition();
                     lDef.LayerID = 0;
                     lDef.Definition = strlDef;

                     // Create an ObservableCollection and add the .Definition to it.
                     System.Collections.ObjectModel.ObservableCollection<LayerDefinition> myObservableCollection2 =
                        new System.Collections.ObjectModel.ObservableCollection<LayerDefinition>();
                     myObservableCollection2.Add(lDef);
                     ArcGISDynamicMapServiceLayer dynamicServiceLayer = MapApplication.Current.Map.Layers[layerID] as ArcGISDynamicMapServiceLayer;
                     dynamicServiceLayer.LayerDefinitions = (myObservableCollection2);

                     dynamicServiceLayer.Refresh();
                 }
             }
0 Kudos