ArcGIS for Silverlight 图形的绘制与编辑基本例子
效果图:
页面代码:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<UserControl x:Class="SilverlightApplication10.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:DesignHeight="300" d:DesignWidth="400" xmlns:esri="http://schemas.esri.com/arcgis/client/2009"> <Grid x:Name="LayoutRoot" Background="White"> <esri:Map Background="White" HorizontalAlignment="Stretch" Name="map1" VerticalAlignment="Stretch" WrapAround="True"> <esri:Map.Layers> <esri:LayerCollection> <esri:ArcGISTiledMapServiceLayer Url="http://services.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer" /> <esri:GraphicsLayer ID="graphicsLayer" x:Name="graphicsLayer" /> </esri:LayerCollection> </esri:Map.Layers> </esri:Map> <Button Content="绘制图形" Height="23" HorizontalAlignment="Left" Margin="12,21,0,0" Name="button1" VerticalAlignment="Top" Width="124" Click="button1_Click" /> <TextBox HorizontalAlignment="Right" Margin="0,12,12,52" Name="textBox1" Width="227" AcceptsReturn="True" VerticalScrollBarVisibility="Auto" /> <Button Content="编辑图形" Height="23" HorizontalAlignment="Left" Margin="12,50,0,0" Name="button2" VerticalAlignment="Top" Width="124" Click="button2_Click" /> </Grid> </UserControl>
后台代码:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Windows; using System.Windows.Controls; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Animation; using System.Windows.Shapes; using ESRI.ArcGIS.Client; using ESRI.ArcGIS.Client.Symbols; using ESRI.ArcGIS.Client.Geometry; namespace SilverlightApplication10 { /// <summary> /// ArcGIS for Silverlight 图形的绘制与编辑基本例子 /// </summary> public partial class MainPage : UserControl { public MainPage() { InitializeComponent(); // 用于编辑 edit = new EditGeometry { Map = map1 }; // 用于绘制 draw = new Draw { FillSymbol = graphicSymbol, Map = map1 }; } public EditGeometry edit; public Draw draw; public Graphic grahpic; public SimpleFillSymbol graphicSymbol = new SimpleFillSymbol() { Fill = new SolidColorBrush(Color.FromArgb(33, 00, 255, 00)), BorderBrush = new SolidColorBrush(Colors.Green), BorderThickness = 2 }; private void button1_Click(object sender, RoutedEventArgs e) { textBox1.Text += Environment.NewLine + "开始绘制图形 ..."; draw.DrawComplete -= new EventHandler<DrawEventArgs>(draw_DrawComplete); draw.DrawComplete += new EventHandler<DrawEventArgs>(draw_DrawComplete); draw.DrawMode = DrawMode.Polygon; draw.IsEnabled = true; } void draw_DrawComplete(object sender, DrawEventArgs e) { textBox1.Text += Environment.NewLine + "绘制图形结束 ..."; draw.DrawComplete -= new EventHandler<DrawEventArgs>(draw_DrawComplete); draw.IsEnabled = false; // 创建对象 grahpic = new Graphic { Geometry = e.Geometry, Symbol = graphicSymbol }; // 添加到地图 GraphicsLayer layer1 = map1.Layers["graphicsLayer"] as GraphicsLayer; layer1.Graphics.Add(grahpic); } private void button2_Click(object sender, RoutedEventArgs e) { if (grahpic != null) { textBox1.Text += Environment.NewLine + "开始编辑图形 ..."; // 添加事件 edit.IsEnabled = true; edit.StartEdit(grahpic); edit.GeometryEdit -= new EventHandler<EditGeometry.GeometryEditEventArgs>(edit_GeometryEdit); edit.GeometryEdit += new EventHandler<EditGeometry.GeometryEditEventArgs>(edit_GeometryEdit); } else { textBox1.Text += Environment.NewLine + "请先绘制图形!"; } } void edit_GeometryEdit(object sender, EditGeometry.GeometryEditEventArgs e) { textBox1.Text += Environment.NewLine + "图形编辑类别:" + e.Action; // 取消编辑或完成编辑时,改变编辑对象的状态 if (e.Action.Equals(EditGeometry.Action.EditCanceled) || e.Action.Equals(EditGeometry.Action.EditCompleted)) { textBox1.Text += Environment.NewLine + "编辑图形结束 ..."; edit.GeometryEdit -= new EventHandler<EditGeometry.GeometryEditEventArgs>(edit_GeometryEdit); edit.IsEnabled = false; edit.StopEdit(); } } } }
本实例采用VS2010+Silverlight 5.0编写,如需源码请点击 SilverlightApplication10.zip 下载。