Map中的圖形繪制
1、說明
圖形繪制首先需要創建一個 GraphicsLayer,然后將 Graphic 添加上去以顯示數據。多數情況下,你將由通過執行查詢返回的結果、在地圖上繪制圖形等方式得到的幾何體生成 Graphic。
添加 Graphic 的主要步驟包括:
1) 獲取添加 Graphic 的目標 GraphicsLayer;
2) 創建或者獲取 Graphic;
3) 設置 Graphic 的 Geometry 屬性;
4) 為 Graphic 應用符號;
5) 將 Graphic 添加到 GraphicsLayer。
Graphic 對象表示可以在 GraphicsLayer 上繪制的要素,同時 FeatureLayer 中的要素、幾何服務操作的參數等大多以 Graphic 對象來表示。
Graphic對象主要成員如下:
屬性:
Attributes:要素的屬性字典(key-value,key是屬性名稱,value是屬性值)。
Geometry:獲取或設置要素的圖形幾何體。
MapTip:獲取或設置當鼠標懸停在要素上方時顯示的地圖提示。
Selected:獲取或設置要素是否被選中。
Symbol:獲取或設置用於渲染當前要素的符號。
方法:
Select/Unselect:選擇/取消選擇當前要素。
事件:
AttbuteValueChanged:當前要素屬性發生變化時觸發的事件。
鼠標相關事件:MouseEnter/MouseLeave、MouseLeftButtonDown、MouseLeftButtonUp、MouseRightButtonDown、MouseRightButtonDown、MouseMove
Graphic對象的不同主要設置Geometry以及Symbol的值。
Geometry包括:MapPoint(點對象)、MultiPoint(多點對象)、Polyline(多義線)、Envelope(矩形對象,長寬方向分別平行於X、Y)、Polygon(多邊形對象,由環Ring組合而成)
2、代碼
MainPage.xaml:
<UserControl x:Class="SilverlightApplication1.MainPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:esri="http://schemas.esri.com/arcgis/client/2009" mc:Ignorable="d" d:DesignHeight="700" d:DesignWidth="718" xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"> <Grid x:Name="LayoutRoot" Background="White" Height="700" Width="717"> <Grid.Resources> <esri:SimpleFillSymbol x:Name = "ResultsFillSymbol" Fill="#500000FF" BorderBrush = "Blue" BorderThickness = "1" /> <esri:SimpleFillSymbol x:Name="DefaultFillSymbol" BorderBrush="Black" BorderThickness="1"/> <esri:PictureMarkerSymbol x:Key="NorthPictureSymbol" Source="/SilverlightApplication1;component/north.png" /> </Grid.Resources> <esri:Map x:Name="MyMap" Margin="279,56,159,112" IsLogoVisible="False" Height="521" Width="358" > <esri:Map.Layers> <esri:ArcGISDynamicMapServiceLayer ID="TestMap" InitializationFailed="ArcGISDynamicMapServiceLayer_InitializationFailed" Url="http://localhost:6080/arcgis/rest/services/test/MapServer" /> <esri:GraphicsLayer ID="drawLayer"/> </esri:Map.Layers> </esri:Map> </Grid> </UserControl>
關鍵.cs Code:
GraphicsLayer _drawGraphicsLayer = MyMap.Layers["drawLayer"] as GraphicsLayer; Graphic resultFeature=new Graphic(); _drawGraphicsLayer.Graphics.Add(resultFeature); ESRI.ArcGIS.Client.Geometry.Geometry geo = resultFeature.Geometry;
//添加圖片 private void AddPictureMarkerGraphics(GraphicsLayer graphicsLayer) { double x_pic = geo.Extent.XMax - 25; double y_pic = geo.Extent.YMax + 50.0; Graphic graphic = new Graphic() { Geometry = new MapPoint(x_pic, y_pic), Symbol = LayoutRoot.Resources["NorthPictureSymbol"] as Symbol }; graphic.Geometry.SpatialReference = new SpatialReference(2364); graphicsLayer.Graphics.Add(graphic); }
ESRI.ArcGIS.Client.Geometry.Geometry geo = resultFeature.Geometry; //添加文字 private void AddTextMarkerGraphics(GraphicsLayer graphicsLayer, string txtTitle) { double x_txt = geo.Extent.XMax - geo.Extent.Width / 2 - 50 ; double y_txt = geo.Extent.YMax + 70.0; TextSymbol textSymbol = new TextSymbol() { FontFamily = new System.Windows.Media.FontFamily("Arial"), Foreground = new System.Windows.Media.SolidColorBrush(Colors.Black), FontSize = 20, Text = txtTitle }; Graphic graphicText = new Graphic() { Geometry = new MapPoint(x_txt, y_txt), Symbol = textSymbol }; graphicText.Geometry.SpatialReference = new SpatialReference(2364); graphicsLayer.Graphics.Add(graphicText); }
//添加多邊形(矩形框) private void AddPolygonGraphics(GraphicsLayer graphicsLayer) { MapPoint point1 = new MapPoint(geo.Extent.XMax + 50.0, geo.Extent.YMax + 80.0); MapPoint point2 = new MapPoint(geo.Extent.XMax + 50.0, geo.Extent.YMin - 80.0); MapPoint point3 = new MapPoint(geo.Extent.XMin - 50.0, geo.Extent.YMin - 80.0); MapPoint point4 = new MapPoint(geo.Extent.XMin - 50.0, geo.Extent.YMax + 80.0); List<MapPoint> pointList = new List<MapPoint>(); pointList.Add(point1); pointList.Add(point2); pointList.Add(point3); pointList.Add(point4); pointList.Add(point1); ESRI.ArcGIS.Client.Geometry.PointCollection points = new ESRI.ArcGIS.Client.Geometry.PointCollection(pointList); ESRI.ArcGIS.Client.Geometry.Polygon polygon = new ESRI.ArcGIS.Client.Geometry.Polygon(); polygon.Rings.Add(points); graphic.Geometry = polygon; graphic.Symbol = LayoutRoot.Resources["DefaultFillSymbol"] as Symbol; graphic.Geometry.SpatialReference = new SpatialReference(2364); graphicsLayer.Graphics.Add(graphic); }