其他分享
首页 > 其他分享> > Arcgis For Silverlight 绘图工具 Draw

Arcgis For Silverlight 绘图工具 Draw

作者:互联网

创建一个Silverlight工程,在默认的MainPage.xaml(2.0时是叫Page.xaml)里,编写代码如下:
<UserControl x:Class="SilverlightApplication3.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" 
    mc:Ignorable="d" d:DesignWidth="640" d:DesignHeight="480"
    xmlns:sysmbol="clr-namespace:ESRI.ArcGIS.Client.Symbols;assembly=ESRI.ArcGIS.Client"
    xmlns:esri="clr-namespace:ESRI.ArcGIS.Client;assembly=ESRI.ArcGIS.Client">
   <Grid x:Name="LayoutRoot">
       <!--地图控件-->
        <esri:Map x:Name="MyMap">
            <esri:Map.Layers>
                <!--添加一个底图-->
                <esri:ArcGISTiledMapServiceLayer
                    Url="http://server.arcgisonline.com/ArcGIS/rest/services/ESRI_Imagery_World_2D/MapServer"/>
                <!--添加一个绘画图层-->
                <esri:GraphicsLayer ID="MyGraphicsLayer"/>
            </esri:Map.Layers>
        </esri:Map>
       <!--END:地图控件-->
       <!--工具栏-->
        <Canvas VerticalAlignment="Top" Width="215" Height="50"  
                HorizontalAlignment="Left" Margin="20,20,0,0">
            <!--工具栏边框效果-->
            <Rectangle  RadiusX="10" RadiusY="10"
                   Fill="#22000000" Canvas.Left="5" Canvas.Top="5"
                   Width="215" Height="50"/>
            <Rectangle   RadiusX="10" RadiusY="10" 
                   Fill="#775c90b2" Canvas.Left="0" Canvas.Top="0"
                   Width="215" Height="50" Stroke="Gray"/>
            <Rectangle RadiusX="5" RadiusY="5"
                   Fill="#FFFFFFFF" Canvas.Left="10" Canvas.Top="10"
                   Width="195" Height="30" Stroke="DarkGreen"/>
            <!--工具-->
            <StackPanel Canvas.Left="12" Canvas.Top="12" Orientation="Horizontal">
                <Button Content="点" Height="25" Width="25" Click="Button_Click"/>
                <Button Content="线" Height="25" Width="25" Click="Button_Click"/>
                <Button Content="框" Height="25" Width="25" Click="Button_Click"/>
                <Button Content="面" Height="25" Width="25" Click="Button_Click"/>
                <Button Content="任意" Height="25" Width="30" Click="Button_Click"/>
                <Button Content="取消" Height="25" Width="30" Click="Button_Click"/>
                <Button Content="清空" Height="25" Width="30" Click="Button_Click"/>
            </StackPanel>
        </Canvas>
       <!--END:工具栏-->


    </Grid>
</UserControl>
绘画要用到一个很重新的类:ESRI.ArcGIS.Client.Draw,它提供了画图的方式。因为这里的几个工具都要用到它,所有我们把它声明为一个全局变量。另外,我们在Map里添加了一个名为MyGraphicsLayer的GraphicsLayer,它是把画的图形显示出来的图层。以下的代码都是在MainPage.xaml.cs文件里添加.
/// <summary>
        /// 全局变量:绘图用
        /// </summary>
        private Draw draw;
        /// <summary>
        /// 全局变量:绘图层
        /// </summary>
        private GraphicsLayer gra;
在初始化函数里,初始化全局变量。这里需要说明的是:Draw的属性中LineSymbol是画线,任意线时所用到的临时符号,FillSymbol则是画框、面时所用到的临时符号。事件DrawComplete固名思义就知道是完成绘制所触发的事件。
public MainPage()
      {
          InitializeComponent();
          //初始化变量 
          gra = this.MyMap.Layers["MyGraphicsLayer"] as GraphicsLayer;
          draw = new Draw(this.MyMap)
          {
              LineSymbol = new LineSymbol()
                { 
                    Color = new SolidColorBrush(Colors.Red), Width = 2 
                },
              FillSymbol=new SimpleFillSymbol()
               {
                  BorderBrush = new SolidColorBrush(Colors.Red) { },
                  BorderThickness = 2,
                  Fill = new SolidColorBrush(Colors.Green) { Opacity = 0.5 }
                },
          };
          //注册事件
          draw.DrawComplete += new EventHandler<DrawEventArgs>(draw_DrawComplete);            
      }
添加DrawComplete事件函数。我们需要在该函数里把所绘制的图形保存在GraphicsLayer里,才能起到显示的效果。Graphic的属性Symbol是显示出来的符号类型,我们根据不同几何形状选择相应的符号。
/// <summary>
/// 绘画完成后,显示出来
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void draw_DrawComplete(object sender, DrawEventArgs e)
{
    gra.Graphics.Add(
        new Graphic() { Geometry=e.Geometry, Symbol=GetSymbol(e.Geometry,Colors.Blue) });
}

/// <summary>
/// 根据几何要素取得对应的渲染符号
/// </summary>
/// <param name="ge"></param>
/// <returns></returns>
public Symbol GetSymbol(ESRI.ArcGIS.Client.Geometry.Geometry ge, Color color)
{
    if (ge is MapPoint)
        return new SimpleMarkerSymbol() 
        { Color = new SolidColorBrush(color), Size = 10, 
          Style = ESRI.ArcGIS.Client.Symbols.SimpleMarkerSymbol.SimpleMarkerStyle.Circle 
        };
    else if (ge is ESRI.ArcGIS.Client.Geometry.Polyline)
        return new LineSymbol()
        { Color = new SolidColorBrush(color), Width = 3 };
    else if (ge is ESRI.ArcGIS.Client.Geometry.Polygon
             || ge is ESRI.ArcGIS.Client.Geometry.Envelope)
    {
        Symbol MySm = new SimpleFillSymbol()
        {
            BorderBrush = new SolidColorBrush(color) { },
            BorderThickness = 3,
            Fill = new SolidColorBrush(Colors.Green) { Opacity = 0.5 }
        };
        return MySm;
    }
    return null;
}
最后就是完成在XAML文件里注册的工具事件函数。需要说明的是:Draw的属性DrawMode即是画图的类型。
/// <summary>
        /// 工具按钮按下后
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Button button = sender as Button;
            string modetype = button.Content.ToString();
            //设置绘画的方式
            switch(modetype)
            {
                case "点":
                 draw.DrawMode = DrawMode.Point;
                 break;
                case "线":
                 draw.DrawMode = DrawMode.Polyline;
                 break;
                case "面":
                 draw.DrawMode = DrawMode.Polygon;
                 break;
                case "框":
                 draw.DrawMode = DrawMode.Rectangle;
                 break;
                case "任意":
                 draw.DrawMode = DrawMode.Freehand;
                 break;
                case "取消":
                 draw.DrawMode = DrawMode.None;
                 break;
                case "清空":
                 gra.Graphics.Clear();
                 return;
            }
            //激活工具
            draw.IsEnabled = true;
        }
编译运行。OK!接下来几节会在这个基础之上,实现放大缩小,点选、框选等功能。

 

标签:case,draw,Geometry,Draw,Silverlight,SolidColorBrush,Arcgis,new,DrawMode
来源: https://www.cnblogs.com/devgis/p/16524738.html