This sample demonstrates using the ArcGIS Runtime SDK for WPF to perform a geometry operation to calculate the location of label points. To use the sample, click the Add New Polygon button, then click subsequent points on the map to draw a polygon. Double-click to complete the polygon. When a polygon is completed, a label point for the polygon will be calculated and shown on the map.
In the code-behind, a LocalGeometryService
created and the url is passed into a GeometryService which is used to first simplify the polygon and then get a label point. When the label point is returned, it is added to the map.
<UserControlx:Class="ArcGISWPFSDK.LocalLabelPoints"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"><Gridx:Name="LayoutRoot"><Grid.Resources><esri:SimpleFillSymbolx:Key="DefaultPolygonSymbol"Fill="#7F0000FF"BorderBrush="Blue"BorderThickness="2"/><esri:PictureMarkerSymbolx:Key="DefaultRasterSymbol"OffsetX="12"OffsetY="12"Source="/Assets/Images/x-24x24.png"/><LinearGradientBrushx:Key="PanelGradient"EndPoint="0.5,1"StartPoint="0.5,0"><LinearGradientBrush.RelativeTransform><TransformGroup><ScaleTransformCenterY="0.5"CenterX="0.5"/><SkewTransformCenterY="0.5"CenterX="0.5"/><RotateTransformAngle="176"CenterY="0.5"CenterX="0.5"/><TranslateTransform/></TransformGroup></LinearGradientBrush.RelativeTransform><GradientStopColor="#FF145787"Offset="0.16"/><GradientStopColor="#FF3D7FAC"Offset="0.502"/><GradientStopColor="#FF88C5EF"Offset="0.984"/></LinearGradientBrush></Grid.Resources><esri:Mapx:Name="MyMap"Extent="-15000000,2000000,-7000000,8000000"Background="#FFE3E3E3"><esri:ArcGISLocalTiledLayerID="BaseMap"Path="..\\Data\\TPKs\\Topographic.tpk"/><esri:GraphicsLayerID="labelGraphicsLayer"x:Name="_labelGraphicsLayer"><esri:GraphicsLayer.MapTip><GridBackground="LightYellow"><StackPanelOrientation="Vertical"Margin="5"><TextBlockText="{Binding [X]}"HorizontalAlignment="Left"Foreground="Black"/><TextBlockText="{Binding [Y]}"HorizontalAlignment="Left"Foreground="Black"/></StackPanel><BorderBorderBrush="Black"BorderThickness="1"/></Grid></esri:GraphicsLayer.MapTip></esri:GraphicsLayer></esri:Map><GridHorizontalAlignment="Right"VerticalAlignment="Top"Margin="0,10,10,0"><RectangleFill="{StaticResource PanelGradient}"Stroke="Gray"RadiusX="10"RadiusY="10"Margin="0,0,0,5"><Rectangle.Effect><DropShadowEffect/></Rectangle.Effect></Rectangle><RectangleFill="#FFFFFFFF"Stroke="DarkGray"RadiusX="5"RadiusY="5"Margin="10,10,10,15"/><StackPanelOrientation="Vertical"Margin="30,25,25,30"><TextBlockx:Name="ResponseText"Text="Create a polygon by clicking on map. The calculated position of a Label Point will be displayed."Width="250"TextAlignment="Left"TextWrapping="Wrap"Foreground="Black"/><Buttonx:Name="ClearGraphicsButton"Content="Clear Polygons"Click="ClearGraphicsButton_Click"/></StackPanel></Grid></Grid></UserControl>
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using ESRI.ArcGIS.Client;
using ESRI.ArcGIS.Client.Geometry;
using ESRI.ArcGIS.Client.Tasks;
using ESRI.ArcGIS.Client.Local;
namespace ArcGISWPFSDK
{
publicpartialclass LocalLabelPoints : UserControl
{
private Draw _drawObject;
GeometryService _geometryTask;
public LocalLabelPoints()
{
InitializeComponent();
LocalGeometryService.GetServiceAsync(lgs =>
{
_geometryTask = new GeometryService();
_geometryTask.Url = lgs.UrlGeometryService;
_geometryTask.SimplifyCompleted += GeometryService_SimplifyCompleted;
_geometryTask.LabelPointsCompleted += GeometryService_LabelPointsCompleted;
_geometryTask.Failed += GeometryService_Failed;
});
MyMap.Layers.LayersInitialized += (s, e) =>
{
_drawObject = new Draw(MyMap)
{
FillSymbol = LayoutRoot.Resources["DefaultPolygonSymbol"] as ESRI.ArcGIS.Client.Symbols.FillSymbol,
DrawMode = DrawMode.Polygon,
IsEnabled = true
};
_drawObject.DrawComplete += DrawObject_DrawComplete;
};
}
privatevoid ClearGraphicsButton_Click(object sender, RoutedEventArgs e)
{
_labelGraphicsLayer.ClearGraphics();
}
privatevoid AddPolygonButton_Click(object sender, RoutedEventArgs e)
{
_drawObject.IsEnabled = true; ;
}
privatevoid DrawObject_DrawComplete(object sender, ESRI.ArcGIS.Client.DrawEventArgs args)
{
_drawObject.IsEnabled = false;
ESRI.ArcGIS.Client.Geometry.Polygon polygon = args.Geometry as ESRI.ArcGIS.Client.Geometry.Polygon;
Graphic graphic = new Graphic()
{
Symbol = LayoutRoot.Resources["DefaultPolygonSymbol"] as ESRI.ArcGIS.Client.Symbols.Symbol,
Geometry = polygon
};
graphic.Attributes.Add("X", "Label Point Polygon");
_labelGraphicsLayer.Graphics.Add(graphic);
List<Graphic> graphicsList = new List<Graphic>();
graphicsList.Add(graphic);
_geometryTask.SimplifyAsync(graphicsList);
}
void GeometryService_SimplifyCompleted(object sender, GraphicsEventArgs e)
{
_geometryTask.LabelPointsAsync(e.Results);
}
privatevoid GeometryService_LabelPointsCompleted(object sender, GraphicsEventArgs args)
{
foreach (Graphic graphic in args.Results)
{
graphic.Symbol = LayoutRoot.Resources["DefaultRasterSymbol"] as ESRI.ArcGIS.Client.Symbols.Symbol;
MapPoint mapPoint = graphic.Geometry as MapPoint;
graphic.Attributes.Add("X", mapPoint.X);
graphic.Attributes.Add("Y", mapPoint.Y);
_labelGraphicsLayer.Graphics.Add(graphic);
}
_drawObject.IsEnabled = true;
}
privatevoid GeometryService_Failed(object sender, TaskFailedEventArgs e)
{
MessageBox.Show("Geometry Service error: " + e.Error);
_drawObject.IsEnabled = true;
}
}
}
Imports System.Collections.Generic
Imports System.Windows
Imports System.Windows.Controls
Imports ESRI.ArcGIS.Client
Imports ESRI.ArcGIS.Client.Geometry
Imports ESRI.ArcGIS.Client.Tasks
Imports ESRI.ArcGIS.Client.Local
Namespace ArcGISWPFSDK
PartialPublicClass LocalLabelPoints
Inherits UserControl
Private MyDrawObject As Draw
Private graphicsLayer As GraphicsLayer
Private _geometryService As GeometryService
PublicSubNew()
InitializeComponent()
Dim localGeometryService AsNew LocalGeometryService()
AddHandler localGeometryService.StartCompleted, Function(sender, eventargs)
Dim lgs As LocalGeometryService = TryCast(sender, LocalGeometryService)
_geometryService = New GeometryService()
_geometryService.Url = lgs.UrlGeometryService
AddHandler _geometryService.SimplifyCompleted, AddressOf GeometryService_SimplifyCompleted
AddHandler _geometryService.LabelPointsCompleted, AddressOf GeometryService_LabelPointsCompleted
AddHandler _geometryService.Failed, AddressOf GeometryService_Failed
MyDrawObject = New Draw(MyMap) With { _
.FillSymbol = TryCast(LayoutRoot.Resources("DefaultPolygonSymbol"), ESRI.ArcGIS.Client.Symbols.FillSymbol), _
.DrawMode = DrawMode.Polygon, _
.IsEnabled = True _
}
AddHandler MyDrawObject.DrawComplete, AddressOf MyDrawObject_DrawComplete
EndFunction
localGeometryService.StartAsync()
graphicsLayer = TryCast(MyMap.Layers("MyGraphicsLayer"), GraphicsLayer)
EndSubPrivateSub ClearGraphicsButton_Click(sender AsObject, e As RoutedEventArgs)
graphicsLayer.ClearGraphics()
EndSubPrivateSub AddPolygonButton_Click(sender AsObject, e As RoutedEventArgs)
MyDrawObject.IsEnabled = TrueEndSubPrivateSub MyDrawObject_DrawComplete(sender AsObject, args As ESRI.ArcGIS.Client.DrawEventArgs)
MyDrawObject.IsEnabled = FalseDim polygon As ESRI.ArcGIS.Client.Geometry.Polygon = TryCast(args.Geometry, ESRI.ArcGIS.Client.Geometry.Polygon)
Dim graphic AsNew Graphic() With { _
.Symbol = TryCast(LayoutRoot.Resources("DefaultPolygonSymbol"), ESRI.ArcGIS.Client.Symbols.Symbol), _
.Geometry = polygon _
}
graphic.Attributes.Add("X", "Label Point Polygon")
graphicsLayer.Graphics.Add(graphic)
Dim graphicsList AsNew List(Of Graphic)()
graphicsList.Add(graphic)
_geometryService.SimplifyAsync(graphicsList)
EndSubPrivateSub GeometryService_SimplifyCompleted(sender AsObject, e As GraphicsEventArgs)
_geometryService.LabelPointsAsync(e.Results)
EndSubPrivateSub GeometryService_LabelPointsCompleted(sender AsObject, args As GraphicsEventArgs)
ForEach graphic As Graphic In args.Results
graphic.Symbol = TryCast(LayoutRoot.Resources("DefaultRasterSymbol"), ESRI.ArcGIS.Client.Symbols.Symbol)
Dim mapPoint As MapPoint = TryCast(graphic.Geometry, MapPoint)
graphic.Attributes.Add("X", mapPoint.X)
graphic.Attributes.Add("Y", mapPoint.Y)
graphicsLayer.Graphics.Add(graphic)
Next
MyDrawObject.IsEnabled = TrueEndSubPrivateSub GeometryService_Failed(sender AsObject, e As TaskFailedEventArgs)
MessageBox.Show("Geometry Service error: " & Convert.ToString(e.[Error]))
MyDrawObject.IsEnabled = TrueEndSubEndClassEndNamespace