Visual Basic (Declaration) | |
---|---|
Public Property Layers As LayerCollection |
C# | |
---|---|
public LayerCollection Layers {get; set;} |
XAML | Copy Code |
---|---|
<!-- Add the following types of Layers to the Map Control: ArcGISTiledMapsServiceLayer, FeatureLayer, and ArcGISDynamicServiceLayer. Zoom to Southern California. --> <esri:Map Name="Map1" Background="White" Margin="12,12,0,0" Height="331" Width="883" Extent="-121,30,-113,38"> <esri:Map.Layers> <esri:ArcGISTiledMapServiceLayer Url="http://services.arcgisonline.com/ArcGIS/rest/services/ESRI_StreetMap_World_2D/MapServer"/> <esri:FeatureLayer Url="http://serverapps.esri.com/ArcGIS/rest/services/California/MapServer/8"/> <esri:ArcGISDynamicMapServiceLayer ID="California Points" Url="http://serverapps.esri.com/ArcGIS/rest/services/California/MapServer"/> </esri:Map.Layers> </esri:Map> <!-- Obtain the Layers via their index number and ID. Display various information about each layer in TextBlocks. --> <TextBlock Name="TB_NumberOfLayers" Text="{Binding ElementName=Map1, Path=Layers.Count}" Height="23" Width="56" Margin="12,349,839,68" /> <TextBlock Name="TB_FirstLayer_Type" Text="{Binding ElementName=Map1, Path=Layers[0]}" Height="23" Width="281" Margin="12,366,614,52"/> <TextBlock Name="TB_SecondLayer_Visible" Text="{Binding ElementName=Map1, Path=Layers[1].Visible}" Height="23" Width="31" Margin="12,385,864,32" /> <TextBlock Name="TB_ThirdLayer_Opacity" Text="{Binding ElementName=Map1, Path=Layers[California Points].Opacity}" Height="23" Width="31" Margin="12,405,864,12"/> |
C# | Copy Code |
---|---|
public void AddLayers() { // Clear out any existing layers. Map1.Layers.Clear(); // Add an ArcGISTiledMapsServiceLayer to the Map. ESRI.ArcGIS.Client.ArcGISTiledMapServiceLayer myArcGISTiledMapServiceLayer = new ESRI.ArcGIS.Client.ArcGISTiledMapServiceLayer(); myArcGISTiledMapServiceLayer.Url = "http://services.arcgisonline.com/ArcGIS/rest/services/ESRI_StreetMap_World_2D/MapServer"; Map1.Layers.Add(myArcGISTiledMapServiceLayer); // Add a FeatureLayer to the Map. ESRI.ArcGIS.Client.FeatureLayer myFeatureLayer = new ESRI.ArcGIS.Client.FeatureLayer(); myFeatureLayer.Url = "http://serverapps.esri.com/ArcGIS/rest/services/California/MapServer/8"; Map1.Layers.Add(myFeatureLayer); // Add an ArcGISDynamicServiceLayer to the Map. ESRI.ArcGIS.Client.ArcGISDynamicMapServiceLayer myArcGISDynamicMapServiceLayer = new ESRI.ArcGIS.Client.ArcGISDynamicMapServiceLayer(); myArcGISDynamicMapServiceLayer.Url = "http://serverapps.esri.com/ArcGIS/rest/services/California/MapServer"; myArcGISDynamicMapServiceLayer.ID = "California Points"; Map1.Layers.Add(myArcGISDynamicMapServiceLayer); // Zoom into the Southern California. Map1.Extent = new ESRI.ArcGIS.Client.Geometry.Envelope(-121, 30, -113, 38); } public void GetLayerInfomation() { // Obtain the first Layer in the LayerCollection via it's index number. ESRI.ArcGIS.Client.Layer firstLayer = Map1.Layers[0]; // Obtain the second Layer in the LayerCollection via iterating over the collection. ESRI.ArcGIS.Client.Layer secondLayer = null; foreach (object x in Map1.Layers) { if (x is ESRI.ArcGIS.Client.FeatureLayer) { secondLayer = (ESRI.ArcGIS.Client.Layer)x; } } // Obtain the third Layer in the LayerCollection via it's ID (string name). ESRI.ArcGIS.Client.Layer thirdLayer = Map1.Layers["California Points"]; // Display various information about each layer in a MessageBox. System.Text.StringBuilder sb_LayerInfo = new System.Text.StringBuilder(); sb_LayerInfo.Append("The number of layers in the Map: " + Map1.Layers.Count.ToString() + System.Environment.NewLine + System.Environment.NewLine); sb_LayerInfo.Append("The 'firstLayer' is of Type: " + firstLayer.GetType().ToString() + System.Environment.NewLine); sb_LayerInfo.Append("The 'secondLayer' is Visible: " + secondLayer.Visible.ToString() + System.Environment.NewLine); sb_LayerInfo.Append("The 'thirdLayer' has an Opacity of: " + thirdLayer.Opacity.ToString()); MessageBox.Show(sb_LayerInfo.ToString()); } |
VB.NET | Copy Code |
---|---|
Public Sub AddLayers() ' Clear out any existing layers. Map1.Layers.Clear() ' Add an ArcGISTiledMapsServiceLayer to the Map. Dim myArcGISTiledMapServiceLayer As New ESRI.ArcGIS.Client.ArcGISTiledMapServiceLayer myArcGISTiledMapServiceLayer.Url = "http://services.arcgisonline.com/ArcGIS/rest/services/ESRI_StreetMap_World_2D/MapServer" Map1.Layers.Add(myArcGISTiledMapServiceLayer) ' Add a FeatureLayer to the Map. Dim myFeatureLayer As New ESRI.ArcGIS.Client.FeatureLayer myFeatureLayer.Url = "http://serverapps.esri.com/ArcGIS/rest/services/California/MapServer/8" Map1.Layers.Add(myFeatureLayer) ' Add an ArcGISDynamicServiceLayer to the Map. Dim myArcGISDynamicMapServiceLayer As New ESRI.ArcGIS.Client.ArcGISDynamicMapServiceLayer myArcGISDynamicMapServiceLayer.Url = "http://serverapps.esri.com/ArcGIS/rest/services/California/MapServer" myArcGISDynamicMapServiceLayer.ID = "California Points" Map1.Layers.Add(myArcGISDynamicMapServiceLayer) ' Zoom into the Southern California. Map1.Extent = New ESRI.ArcGIS.Client.Geometry.Envelope(-121, 30, -113, 38) End Sub Public Sub GetLayerInfomation() ' Obtain the first Layer in the LayerCollection via it's index number. Dim firstLayer As ESRI.ArcGIS.Client.Layer = Map1.Layers(0) ' Obtain the second Layer in the LayerCollection via iterating over the collection. Dim secondLayer As ESRI.ArcGIS.Client.Layer = Nothing Dim x As Object For Each x In Map1.Layers If TypeOf x Is ESRI.ArcGIS.Client.FeatureLayer Then secondLayer = x End If Next ' Obtain the third Layer in the LayerCollection via it's ID (string name). Dim thirdLayer As ESRI.ArcGIS.Client.Layer = Map1.Layers("California Points") ' Display various information about each layer in a MessageBox. Dim sb_LayerInfo As New System.Text.StringBuilder sb_LayerInfo.Append("The number of layers in the Map: " + Map1.Layers.Count.ToString + vbCrLf + vbCrLf) sb_LayerInfo.Append("The 'firstLayer' is of Type: " + firstLayer.GetType.ToString + vbCrLf) sb_LayerInfo.Append("The 'secondLayer' is Visible: " + secondLayer.Visible.ToString + vbCrLf) sb_LayerInfo.Append("The 'thirdLayer' has an Opacity of: " + thirdLayer.Opacity.ToString) MessageBox.Show(sb_LayerInfo.ToString) End Sub |
The Layers Property always returns a LayerCollection object; it is never Nothing/null. If no layers are added to the Map the LayerCollection.Count = 0.
Do not confuse the results of a Map.Layers Property (which returns a LayerCollection object) with the ArcGISTiledMapServiceLayer.Layers and ArcGISDynamicMapServicelayer.Layers Properties (which return a LayerInfo() object). The LayerInfo() is an array of LayerInfo objects and is often referred to in the documentation as a ‘sub-layers’.
Note: Accessing a specific Layer via XAML can be done with or without drilling through the <esri:Map.Layers> tag. The following two abbreviated XAML code fragments are equivalent:
<Grid> <esri: Map> <esri:Map.Layers> <esri:ArcGISDynamicMapServiceLayer/> </esri:Map.Layers> </esri:Map> </Grid>
<Grid> <esri: Map> <esri:ArcGISDynamicMapServiceLayer/> </esri:Map> </Grid>
Accessing an individual Layer in the LayerCollection in code-behind can be done either by its index number, ID (string name), or via iterating over the LayerCollection. In XAML it is not possible to iterate over a collection using a For/Each loop so accessing a Layer in the LayerCollection is done using either its index number or ID (string name). When using XAML is may be necessary to escape special characters when accessing a Layer via it’s ID (string name); for example a ‘space’ in the name should be escaped with  . The screen shot and code example in this document demonstrates the alternatives for accessing Layers in a LayerCollection.
Target Platforms: Windows XP Professional, Windows Server 2003 family, Windows Vista, Windows Server 2008 family