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; //得到顯示的圖層ID int[] 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="http://sampleserver3.arcgisonline.com/ArcGIS/rest/services/Earthquakes/Since_1970/MapServer" /> <esri:ArcGISDynamicMapServiceLayer.LayerDefinitions> <!-- LayerID="0" is the Earthquakes1970 layer. The Definition only displays earth quakes that have a Magnitude 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 a Magnitude 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設置的LayerDefinition System.Collections.ObjectModel.ObservableCollection<LayerDefinition> myObservableCollection2 = new System.Collections.ObjectModel.ObservableCollection<LayerDefinition>(); myObservableCollection2.Add(myDefinition); //啟動設置的LayerDefinition myArcGISDynamicMapServiceLayer.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()函數來刷新地圖才能看到效果。
參考: