ArcGIS API for Silverlight map中添加graphic对象(文字、几何图形、图片)

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);
        }