private void GeometryService_AreasAndLengthsCompleted(object sender, AreasAndLengthsEventArgs args) { //TODO: Geometry service not returning correct values, area unit seems invalid, may need to use data in //planar coordinate space // convert results from meters into miles and sq meters into sq miles for our display double miles = args.Results.Lengths[0] * 0.0006213700922; double sqmi = Math.Abs(args.Results.Areas[0]) * 0.0000003861003; ResponseTextBlock.Text = String.Format("Polygon area: {0} sq. miles\nPolygon perimeter: {1} miles.", Math.Round(sqmi, 3), Math.Round(miles, 3)); }
<esri:Editor x:Key="MyEditor" EditCompleted="Editor_EditCompleted" Map="{Binding ElementName=MyMap}" LayerIDs="MyGraphicsLayer"/>
<Button x:Name="AddBtn" Content="Add" DataContext="{StaticResource MyEditor}" Command="{Binding Add}" CommandParameter="{StaticResource DefaultFillSymbol}"/>
GeometryService geometryService; private void Editor_EditCompleted(object sender, Editor.EditEventArgs e) { if (e.Action == Editor.EditAction.Add) { foreach (var edit in e.Edits) { geometryService.AreasAndLengthsAsync(new List<Graphic>(){edit.Graphic}); break; } } }
geometryService = new GeometryService("http://sampleserver3.arcgisonline.com/ArcGIS/rest/services/Geometry/GeometryServer"); geometryService.AreasAndLengthsCompleted += GeometryService_AreasAndLengthsCompleted; geometryService.Failed += GeometryService_Failed;
#region Cauculate Area Poly ////Calculate Area private void MyDrawObject_DrawBegin(object sender, EventArgs args) { GraphicsLayer graphicsLayer = Map.Layers["MyGraphicsLayer"] as GraphicsLayer; graphicsLayer.ClearGraphics(); } private void MyDrawObject_DrawComplete(object sender, DrawEventArgs args) { ESRI.ArcGIS.Client.Geometry.Polygon polygon = args.Geometry as ESRI.ArcGIS.Client.Geometry.Polygon; polygon.SpatialReference = new SpatialReference(54004); Graphic graphic = new Graphic() { Symbol = LayoutRoot.Resources["DefaultFillSymbol"] as ESRI.ArcGIS.Client.Symbols.Symbol, Geometry = polygon, }; GeometryService geometryService = new GeometryService("http://sampleserver3.arcgisonline.com/ArcGIS/rest/services/Geometry/GeometryServer"); geometryService.AreasAndLengthsCompleted += GeometryService_AreasAndLengthsCompleted; geometryService.Failed += GeometryService_Failed; GraphicsLayer graphicsLayer = Map.Layers["MyGraphicsLayer"] as GraphicsLayer; graphicsLayer.Graphics.Add(graphic); List<Graphic> graphicList = new List<Graphic>(); graphicList.Add(graphic); geometryService.AreasAndLengthsAsync(graphicList); }
polygon.SpatialReference = new SpatialReference(54004);
Setting the spatial reference by code is not enough because by doing this, the coordinates of the geometry don't change.
You have to project the coordinates by using a geometry service.
The example is only projection one point and obviously the drawn area (polygon) or line could/will have several (undefined) number of points.