ArcGIS可以设置动态地图服务(ArcGISDynamicMapServiceLayer)显示哪些图层,也可以设置每个图层根据某个属性字段的某些条件来进行过滤显示。
1、设置显示的图层
主要是通过ArcGISDynamicMapServiceLayer的VisibleLayers属性来设置或得到当前显示的图层,C#代码如下:
代码中Map1和TextBlock_VisibleLayers是已经定义好的地图和TextBlock控件。
//ArcGISDynamicMapServiceLayer初始化函数private void ArcGISDynamicMapServiceLayer_Initialized(object sender, System.EventArgs e){//得到ArcGISDynamicMapServiceLayer,这里是第一个图层,根据实际情况,可以通过Map.Layers["图层的ID"]来得到 ArcGISDynamicMapServiceLayer myArcGISDynamicMapServiceLayer = (ArcGISDynamicMapServiceLayer)Map1.Layers[0];//VisibleLayers的读写//下面注释的代码:设置myArcGISDynamicMapServiceLayer第二和第三个图层显示 //int[] myVisibleLayers2 = { 1, 2 }; //myArcGISDynamicMapServiceLayer.VisibleLayers = myVisibleLayers2;//得到显示的图层IDint[] myVisibleLayers = myArcGISDynamicMapServiceLayer.VisibleLayers;if (myVisibleLayers != null){string myVisibleLayersText = "Number VisibleLayers: " + myVisibleLayers.Length.ToString();string myVisibleLayersText2 = "";int i = 0;for (i = 0; i < myVisibleLayers.Length; ++i){myVisibleLayersText2 = myVisibleLayersText2 + " " + myVisibleLayers[i].ToString();}TextBlock_VisibleLayers.Text = myVisibleLayersText + ". VisibleLayers ID's: " + myVisibleLayersText2;}else{TextBlock_VisibleLayers.Text = "[VisibleLayers not set - Meaning all layers are visible.]";}}
2、设置图层根据字段条件过滤显示
如果不全部显示一个图层的所有要素,而是根据某些条件来“过滤显示”,则可以通过设置LayerDefinition来实现。
LayerDefinition的设置可以通过XAML代码实现:
<StackPanel Name="StackPanel1" Height="400" Width="400" Margin="0,0,0,0" HorizontalAlignment="Left" VerticalAlignment="Top" ><esri:Map Background="White" Name="Map1" Height="200" Width="400"><!-- Set the LayerDefinition for the ArcGISDynamicMapServiceLayer. --><!-- By default no LayerDefinition is set unless explicitly set on the server. --><esri:ArcGISDynamicMapServiceLayer Url="/ArcGIS/rest/services/Earthquakes/Since_1970/MapServer" /><esri:ArcGISDynamicMapServiceLayer.LayerDefinitions><!-- LayerID="0" is the Earthquakes1970 layer. The Definition only displays earth quakes that have aMagnitude greater than 6. The field Magnitude is of type esriFieldTypeDouble. --><esri:LayerDefinition LayerID="0" Definition="Magnitude > 6" /><!-- LayerID="0" is the Earthquakes1970 layer. The Definition only displays earth quakes that have aMagnitude greater than 3 and less than 6. The field Magnitude is of type esriFieldTypeDouble. --><!-- <esri:LayerDefinition LayerID="0" Definition="Magnitude > 3 AND Magnitude < 6" /> --><!-- Another example where the Name of the earth quake event is to contains the letters 'CHINA'.Note: the Definition is case sensitive. The field Name is of type esriFieldTypeString. --><!-- <esri:LayerDefinition LayerID="0" Definition="Name LIKE 'CHINA'" /> --><!-- Another example where the Name of the earth quake event is exactly matches the letters 'VENEZUELA'.Note: the Definition is case sensitive. The field Name is of type esriFieldTypeString. --><!-- <esri:LayerDefinition LayerID="0" Definition="Name = 'VENEZUELA'" /> --><!-- Another example where the earth quake events are displayed if they occured after January 15, 2000. The field Date_ is of type esriFieldTypeDate. --><!-- <esri:LayerDefinition LayerID="0" Definition="Date_ > DATE '1/15/2000'" />--></esri:ArcGISDynamicMapServiceLayer.LayerDefinitions></esri:ArcGISDynamicMapServiceLayer></esri:Map><!-- LayerDefinitions Property (Read) --><TextBlock Height="23" Name="TextBlock_LayerDefinitions" Text="{Binding ElementName=Map1, Path=Layers[0].LayerDefinitions[0].Definition}" /></StackPanel>
也可以通过代码来实现,C#代码如下:
//ArcGISDynamicMapServiceLayer初始化函数private void ArcGISDynamicMapServiceLayer_Initialized(object sender, System.EventArgs e){//得到ArcGISDynamicMapServiceLayer,这里是第一个图层,根据实际情况,可以通过Map.Layers["图层的ID"]来得到 ArcGISDynamicMapServiceLayer myArcGISDynamicMapServiceLayer = (ArcGISDynamicMapServiceLayer)Map1.Layers[0];//LayerDefinition的读写//设置图层的LayerDefinition,默认情况下LayerDefinition是没有设置的//得到图层的LayerDefinition ESRI.ArcGIS.Client.LayerDefinition myDefinition = new ESRI.ArcGIS.Client.LayerDefinition();myDefinition.LayerID = 0;//设置LayerDefinition,属性字段“Magnitude”属于ID为0的图层//LayerDefinition的设置语句和Query中的Where语句一样//字段Magnitude的类型是esriFieldTypeDouble.myDefinition.Definition = "Magnitude > 6";// The Definition only displays earth quakes that have a Magnitude greater than 3 and less that 6. // The field Magnitude is of type esriFieldTypeDouble.// myDefinition.Definition = "Magnitude > 3 AND Magnitude < 6";// Another example where the Name of the earth quake event is to contains the letters 'CHINA'.// Note: the Definition is case sensitive. The field Name is of type esriFieldTypeString.//myDefinition.Definition = "Name LIKE 'CHINA'";// Another example where the Name of the earth quake event is exactly matches the letters 'VENEZUELA'.// Note: the Definition is case sensitive. The field Name is of type esriFieldTypeString.//myDefinition.Definition = "Name = 'VENEZUELA'";// Another example where the earth quake events are displayed if they occured after January 15, 2000. // The field Date_ is of type esriFieldTypeDate.//myDefinition.Definition = "Date_ > DATE '1/15/2000'";//创建一个ObservableCollection,add设置的LayerDefinitionSystem.Collections.ObjectModel.ObservableCollection<LayerDefinition> myObservableCollection2 = new System.Collections.ObjectModel.ObservableCollection<LayerDefinition>();myObservableCollection2.Add(myDefinition);//启动设置的LayerDefinitionmyArcGISDynamicMapServiceLayer.LayerDefinitions = myObservableCollection2;//得到已经存在的LayerDefinitions collection.System.Collections.ObjectModel.ObservableCollection<LayerDefinition> myObservableCollection = myArcGISDynamicMapServiceLayer.LayerDefinitions;if (myObservableCollection.Count > 0){//得到第一个LayerDefinition ESRI.ArcGIS.Client.LayerDefinition myLayerDefinition = myObservableCollection[0];//得到LayerDefinition的信息TextBlock_LayerDefinitions.Text = "For Layer: " + myLayerDefinition.LayerID.ToString() + ". The Defintion is: " + myLayerDefinition.Definition;}else{TextBlock_LayerDefinitions.Text = "[NO LayerDefinitions SET]";}}
上面ArcGISDynamicMapServiceLayer_Initialized(object sender, System.EventArgs e)函数是订阅到ArcGISDynamicMapServiceLayer.Initialized的,也就是在图层加载的时候就设置了“过滤”条件,如果要在后期在某个响应事件中动态的刷新地图,需要在设置LayerDefinition后,调用ArcGISDynamicMapServiceLayer.Refresh()函数来刷新地图才能看到效果。
参考:
/en/webapi/silverlight/apiref/ESRI.ArcGIS.Client~ESRI.ArcGIS.Client.ArcGISDynamicMapServiceLayer~VisibleLayers.html
/en/webapi/silverlight/apiref/ESRI.ArcGIS.Client~ESRI.ArcGIS.Client.ArcGISDynamicMapServiceLayer~LayerDefinitions.html