这里控制的是利用ArcGIS Server发布的ArcGISDynamicMapServiceLayer服务,下面的ListBox会加载所有服务图层,通过勾选可以动态控制某一个或某一些图层的显示与否。主要代码如下:
<esri:ArcGISDynamicMapServiceLayer ID="MyLayer" Url="http://192.168.0.89/arcgis/rest/services/AnHuiMap/MapServer/" Initialized="MyLayer_Initialized" />
<!--图层控制--> <!--<Grid HorizontalAlignment="Right" Margin="0,260,0,0" Width="209" Height="200" VerticalAlignment="Top" > <Grid.RowDefinitions> <RowDefinition Height="0.205*"/> <RowDefinition Height="0.795*"/> </Grid.RowDefinitions> <Grid.ColumnDefinitions> <ColumnDefinition Width="0.837*"/> <ColumnDefinition Width="0.163*"/> </Grid.ColumnDefinitions> <Border x:Name="bShowPicControl" BorderThickness="1" Cursor="Hand" Margin="1,0,0,1" Grid.Column="1" Width="33" MouseLeftButtonDown="bShowPicControl_MouseLeftButtonDown" MouseLeftButtonUp="bShowPicControl_MouseLeftButtonUp" Height="34"> <Border.Background> <ImageBrush ImageSource="Images/Layer_control.png" Stretch="None"/> </Border.Background> <ToolTipService.ToolTip> <TextBlock FontSize="13.333" FontFamily="Microsoft YaHei" >图 层 控 制</TextBlock> </ToolTipService.ToolTip> </Border> <Grid x:Name="grid_control" Margin="0,1,0,0" Grid.RowSpan="2" Width="175" Height="199" Visibility="Collapsed"> <Grid Height="34" Background="#FFF3F3F3" Margin="0" Width="174" VerticalAlignment="Top" d:LayoutOverrides="Height"> <Border BorderBrush="#FF0E529B" BorderThickness="1" CornerRadius="5" Height="34" Width="174"> <StackPanel Orientation="Horizontal" Width="164" Height="32"> <CheckBox x:Name="cb_ZM" Content="站 名" Margin="2,5,0,5" Width="54" FontSize="14.667" Cursor="Hand" FontFamily="Microsoft YaHei" VerticalAlignment="Center" HorizontalAlignment="Center" Height="18" Checked="cb_ZM_Checked" Unchecked="cb_ZM_Unchecked"/> <RadioButton x:Name="rb1" Width="54" Margin="0,5" IsChecked="True" FontSize="14.667" Content="数 据" Height="18" GroupName="gn_group" Cursor="Hand" FontFamily="Microsoft YaHei" HorizontalAlignment="Center" VerticalAlignment="Center" Click="rb_Click"/> <RadioButton x:Name="rb2" Width="54" Margin="0,5" FontSize="14.667" Content="站 码" GroupName="gn_group" Cursor="Hand" FontFamily="Microsoft YaHei" HorizontalAlignment="Center" VerticalAlignment="Center" Click="rb_Click" Height="18"/> </StackPanel> </Border> </Grid> <Border Height="163" Margin="0,36,0,0" Width="175" d:LayoutOverrides="GridBox" BorderBrush="#FF0E529B" CornerRadius="5" BorderThickness="1" > <ListBox ItemsSource="{Binding Layers.[MyLayer].Layers, ElementName=myMap}" BorderBrush="{x:Null}" FontSize="13.333" FontFamily="Microsoft YaHei" Width="173" Height="161"> <ListBox.ItemTemplate> <DataTemplate> <CheckBox Margin="2" x:Name="MyLayer" Content="{Binding Name}" IsChecked="{Binding DefaultVisibility}" Tag="{Binding ID}" ClickMode="Press" Click="CheckBox_Click" /> </DataTemplate> </ListBox.ItemTemplate> </ListBox> </Border> </Grid> </Grid>-->
#region 图层控制显示与隐藏 private void MyLayer_Initialized(object sender, EventArgs e) { ESRI.ArcGIS.Client.ArcGISDynamicMapServiceLayer dynamicServiceLayer = sender as ESRI.ArcGIS.Client.ArcGISDynamicMapServiceLayer; if (dynamicServiceLayer.VisibleLayers == null) dynamicServiceLayer.VisibleLayers = GetDefaultVisibleLayers(dynamicServiceLayer); } private int[] GetDefaultVisibleLayers(ESRI.ArcGIS.Client.ArcGISDynamicMapServiceLayer dynamicService) { List<int> visibleLayerIDList = new List<int>(); ESRI.ArcGIS.Client.LayerInfo[] layerInfoArray = dynamicService.Layers; for (int index = 0; index < layerInfoArray.Length; index++) { if (layerInfoArray[index].DefaultVisibility) visibleLayerIDList.Add(index); } return visibleLayerIDList.ToArray(); } private void CheckBox_Click(object sender, RoutedEventArgs e) { CheckBox tickedCheckBox = sender as CheckBox; string serviceName = tickedCheckBox.Name; bool visible = (bool)tickedCheckBox.IsChecked; int layerIndex = (int)tickedCheckBox.Tag; ESRI.ArcGIS.Client.ArcGISDynamicMapServiceLayer dynamicServiceLayer = myMap.Layers[serviceName] as ESRI.ArcGIS.Client.ArcGISDynamicMapServiceLayer; List<int> visibleLayerList = dynamicServiceLayer.VisibleLayers != null ? dynamicServiceLayer.VisibleLayers.ToList() : new List<int>(); if (visible) { if (!visibleLayerList.Contains(layerIndex)) visibleLayerList.Add(layerIndex); } else { if (visibleLayerList.Contains(layerIndex)) visibleLayerList.Remove(layerIndex); } dynamicServiceLayer.VisibleLayers = visibleLayerList.ToArray(); } #endregion