This sample demonstrates use of a GeometryService 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 GeometryService 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.LabelPoints"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="-118.331,33.7,-116.75,34"><esri:ArcGISTiledMapServiceLayerID="StreetMapLayer"Url="http://services.arcgisonline.com/ArcGIS/rest/services/ESRI_StreetMap_World_2D/MapServer"/><esri:GraphicsLayerID="MyGraphicsLayer"><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;
namespace ArcGISWPFSDK
{
publicpartialclass LabelPoints : UserControl
{
private Draw MyDrawObject;
private GeometryService geometryService;
private GraphicsLayer graphicsLayer;
public LabelPoints()
{
InitializeComponent();
MyDrawObject = new Draw(MyMap)
{
FillSymbol = LayoutRoot.Resources["DefaultPolygonSymbol"] as ESRI.ArcGIS.Client.Symbols.FillSymbol,
DrawMode = DrawMode.Polygon,
IsEnabled = true
};
MyDrawObject.DrawComplete += MyDrawObject_DrawComplete;
geometryService = new GeometryService("http://tasks.arcgisonline.com/ArcGIS/rest/services/Geometry/GeometryServer");
geometryService.SimplifyCompleted += GeometryService_SimplifyCompleted;
geometryService.LabelPointsCompleted += GeometryService_LabelPointsCompleted;
geometryService.Failed += GeometryService_Failed;
graphicsLayer = MyMap.Layers["MyGraphicsLayer"] as GraphicsLayer;
}
privatevoid ClearGraphicsButton_Click(object sender, RoutedEventArgs e)
{
graphicsLayer.ClearGraphics();
}
privatevoid AddPolygonButton_Click(object sender, RoutedEventArgs e)
{
MyDrawObject.IsEnabled = true; ;
}
privatevoid MyDrawObject_DrawComplete(object sender, ESRI.ArcGIS.Client.DrawEventArgs args)
{
MyDrawObject.IsEnabled = false;
ESRI.ArcGIS.Client.Geometry.Polygon polygon = args.Geometry as ESRI.ArcGIS.Client.Geometry.Polygon;
polygon.SpatialReference = new SpatialReference(4326);
Graphic graphic = new Graphic()
{
Symbol = LayoutRoot.Resources["DefaultPolygonSymbol"] as ESRI.ArcGIS.Client.Symbols.Symbol,
Geometry = polygon
};
graphic.Attributes.Add("X", "Label Point Polygon");
graphicsLayer.Graphics.Add(graphic);
List<Graphic> graphicsList = new List<Graphic>();
graphicsList.Add(graphic);
geometryService.SimplifyAsync(graphicsList);
}
void GeometryService_SimplifyCompleted(object sender, GraphicsEventArgs e)
{
geometryService.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);
graphicsLayer.Graphics.Add(graphic);
}
MyDrawObject.IsEnabled = true;
}
privatevoid GeometryService_Failed(object sender, TaskFailedEventArgs e)
{
MessageBox.Show("Geometry Service error: " + e.Error);
MyDrawObject.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
Namespace ArcGISWPFSDK
PartialPublicClass LabelPoints
Inherits UserControl
Private MyDrawObject As Draw
Private geometryService As GeometryService
Private graphicsLayer As GraphicsLayer
PublicSubNew()
InitializeComponent()
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
geometryService = New GeometryService("http://tasks.arcgisonline.com/ArcGIS/rest/services/Geometry/GeometryServer")
AddHandler geometryService.SimplifyCompleted, AddressOf GeometryService_SimplifyCompleted
AddHandler geometryService.LabelPointsCompleted, AddressOf GeometryService_LabelPointsCompleted
AddHandler geometryService.Failed, AddressOf GeometryService_Failed
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)
polygon.SpatialReference = New SpatialReference(4326)
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