ArcGIS Runtime SDK for WPF之测量距离和面积
bu不多说,上代码
using System.Windows; using ESRI.ArcGIS.Client; using ESRI.ArcGIS.Client.Tasks; using ESRI.ArcGIS.Client.Local; using System; using System.Windows.Input; using ESRI.ArcGIS.Client.Geometry; using ESRI.ArcGIS.Client.Symbols; using System.Collections.Generic; namespace ArcGISWpfApplication2 { public partial class MainWindow : Window { GeometryService gMeasureLength,gMeasureArea; Draw drawMeasure; public MainWindow() { // License setting and ArcGIS Runtime initialization is done in Application.xaml.cs. InitializeComponent(); GraphicsLayer gLayer = new GraphicsLayer(); gLayer.ID = "MyGraphicsLayerMeasure"; Map.Layers.Add(gLayer); //Map.Layers.Insert(0, gLayer);//这样不行,arcgis runtime加载图层与arcgis mapinfo方式相反,索引大的在上面 LocalGeometryServiceInit(); } void LocalGeometryServiceInit() { LocalGeometryService.GetServiceAsync(localGeometryService => { gMeasureLength = new GeometryService(); gMeasureLength.Url = localGeometryService.UrlGeometryService; gMeasureLength.LengthsCompleted += gMeasureLength_LengthsCompleted; gMeasureLength.Failed += gMeasureLength_Failed; }); LocalGeometryService.GetServiceAsync(localGeometryService => { gMeasureArea = new GeometryService(); gMeasureArea.Url = localGeometryService.UrlGeometryService; gMeasureArea.AreasAndLengthsCompleted += gMeasureArea_AreasAndLengthsCompleted; gMeasureArea.Failed += gMeasureLength_Failed; }); } void gMeasureArea_AreasAndLengthsCompleted(object sender, AreasAndLengthsEventArgs e) { double _Lengths = e.Results.Lengths[0]; double _Area = e.Results.Areas[0]; MessageBox.Show(String.Format("多边形周长为{0},面积为{1}",Math.Round(_Lengths,3),Math.Round(_Area,3))); } void gMeasureLength_Failed(object sender, TaskFailedEventArgs e) { MessageBox.Show("测量失败"); } void gMeasureLength_LengthsCompleted(object sender, LengthsEventArgs e) { MessageBox.Show(String.Format("折线的长度:{0} 公里", Math.Round(e.Results[0], 3))); Map.Cursor = Cursors.Hand; } private void MeasureLine(object sender, RoutedEventArgs e) { GraphicsLayer _GraphicsLayer = Map.Layers["MyGraphicsLayerMeasure"] as GraphicsLayer; _GraphicsLayer.ClearGraphics(); drawMeasure = new Draw(Map) { DrawMode = DrawMode.Polyline, IsEnabled = true, //LineSymbol = Root.Resources["SelectLineSymbol"] as LineSymbol }; drawMeasure.DrawBegin +=drawMeasure_DrawBegin; drawMeasure.DrawComplete +=drawMeasure_DrawDistanceComplete; } void drawMeasure_DrawDistanceComplete(object sender, DrawEventArgs e) { Polyline _Polyline = e.Geometry as Polyline; _Polyline.SpatialReference = Map.SpatialReference; Graphic _Graphic = new Graphic() { Symbol = Resources["CompleteLineSymbol"] as Symbol,//不自定义的话默认的是透明的,不然测量完看不到线 Geometry = _Polyline }; GraphicsLayer _GraphicsLayer = Map.Layers["MyGraphicsLayerMeasure"] as GraphicsLayer; _GraphicsLayer.Graphics.Add(_Graphic); //开始计算长度 gMeasureLength.LengthsAsync(_GraphicsLayer.Graphics, LinearUnit.Kilometer, CalculationType.Geodesic, null); drawMeasure.DrawMode = DrawMode.None; } void drawMeasure_DrawBegin(object sender, EventArgs e) { Map.Cursor=Cursors.Arrow; } //MeasureAreaLength private void MeasureAreaLength(object sender, RoutedEventArgs e) { GraphicsLayer _GraphicsLayer = Map.Layers["MyGraphicsLayerMeasure"] as GraphicsLayer; _GraphicsLayer.ClearGraphics(); drawMeasure = new Draw(Map) { DrawMode = DrawMode.Polygon, IsEnabled = true, //LineSymbol = Root.Resources["SelectLineSymbol"] as LineSymbol }; drawMeasure.DrawBegin += drawMeasure_DrawBegin; drawMeasure.DrawComplete += drawMeasure_DrawAreaLengthComplete; } private void drawMeasure_DrawAreaLengthComplete(object sender, DrawEventArgs e) { Polygon _Polygon = e.Geometry as Polygon; _Polygon.SpatialReference = Map.SpatialReference; Graphic _Graphic = new Graphic() { Symbol = Resources["CompletePolygonSymbol"] as Symbol, Geometry = _Polygon }; GraphicsLayer _GraphicsLayer = Map.Layers["MyGraphicsLayerMeasure"] as GraphicsLayer; _GraphicsLayer.Graphics.Add(_Graphic); List<Graphic> _GraphicList = new List<Graphic>(); _GraphicList.Add(_Graphic); gMeasureArea.AreasAndLengthsAsync(_GraphicList,AreaUnit.SquareKilometers); } } }
<Window x:Class="ArcGISWpfApplication2.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:esri="http://schemas.esri.com/arcgis/client/2009" Title="MainWindow" Height="350" Width="525"> <Window.Resources> <esri:SimpleLineSymbol x:Key="CompleteLineSymbol" Color="Green" Width="4" /> <esri:SimpleFillSymbol x:Key="CompletePolygonSymbol" Fill="Green" BorderBrush="LightBlue" BorderThickness="1" /> </Window.Resources> <Grid> <Grid.RowDefinitions> <RowDefinition Height="auto"></RowDefinition> <RowDefinition Height="*"></RowDefinition> </Grid.RowDefinitions> <Border Background="#ccc"> <StackPanel Orientation="Horizontal"> <Button Height="30" Width="100" Click="MeasureLine">测距</Button> <Button Height="30" Width="100" Click="MeasureAreaLength">测面积</Button> </StackPanel> </Border> <esri:Map x:Name="Map" Grid.Row="1" Background="White" Cursor="Hand" Extent="-20014711, 15, 1656956, 12175318"> <esri:ArcGISTiledMapServiceLayer ID="BaseLayer" Url="http://services.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer" /> <!--<esri:GpsLayer x:Name="MyGpsLayer" />--> </esri:Map> </Grid> </Window>