This sample demonstrates using the ArcGIS Runtime SDK for WPF to perform a geometry operation to calculate a buffer. To use the sample, simply click a point on the map. The click point and a buffer of 1000km around the point will be shown.
In the code-behind, a LocalGeometryService
is started. The url is passed into a GeometryService
which is used to buffer the point. Note that, for accurate results, the BufferSpatialReference is set based on the area of interest.
<UserControlx:Class="ArcGISWPFSDK.LocalBufferPoint"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><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><esri:PictureMarkerSymbolx:Key="DefaultClickSymbol"OffsetX="11"OffsetY="39"Source="/Assets/Images/i_pushpin.png"/><esri:SimpleFillSymbolx:Key="DefaultBufferSymbol"Fill="#660000FF"BorderBrush="Blue"BorderThickness="2"/></Grid.Resources><esri:Mapx:Name="MyMap"MouseClick="MyMap_MouseClick"Extent="-15000000,2000000,-7000000,8000000"Background="#FFE3E3E3"IsEnabled="False"><esri:ArcGISLocalTiledLayerID="BaseMap"Path="..\\Data\\TPKs\\Topographic.tpk"/><esri:GraphicsLayerID="MyGraphicsLayer"/></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"/><TextBlockx:Name="InformationTextBlock"Text="Click on map to set a location. A buffer of 5 degrees will be displayed."Width="200"TextAlignment="Left"Margin="30,20,25,30"TextWrapping="Wrap"Foreground="Black"/></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 LocalBufferPoint : UserControl
{
GeometryService _geometryTask;
public LocalBufferPoint()
{
InitializeComponent();
LocalGeometryService.GetServiceAsync(localGeometryService =>
{
_geometryTask = new GeometryService();
_geometryTask.Url = localGeometryService.UrlGeometryService;
_geometryTask.BufferCompleted += GeometryService_BufferCompleted;
_geometryTask.Failed += GeometryService_Failed;
MyMap.IsEnabled = true;
});
}
privatevoid MyMap_MouseClick(object sender, ESRI.ArcGIS.Client.Map.MouseEventArgs e)
{
GraphicsLayer graphicsLayer = MyMap.Layers["MyGraphicsLayer"] as GraphicsLayer;
graphicsLayer.ClearGraphics();
e.MapPoint.SpatialReference = MyMap.SpatialReference;
Graphic graphic = new ESRI.ArcGIS.Client.Graphic()
{
Geometry = e.MapPoint,
Symbol = LayoutRoot.Resources["DefaultClickSymbol"] as ESRI.ArcGIS.Client.Symbols.Symbol
};
graphic.SetZIndex(1);
graphicsLayer.Graphics.Add(graphic);
BufferParameters bufferParams = new BufferParameters()
{
Unit = LinearUnit.Degree,
BufferSpatialReference = new SpatialReference(4326),
OutSpatialReference = MyMap.SpatialReference
};
bufferParams.Features.Add(graphic);
bufferParams.Distances.Add(5);
_geometryTask.BufferAsync(bufferParams);
}
void GeometryService_BufferCompleted(object sender, GraphicsEventArgs args)
{
IList<Graphic> results = args.Results;
GraphicsLayer graphicsLayer = MyMap.Layers["MyGraphicsLayer"] as GraphicsLayer;
foreach (Graphic graphic in results)
{
graphic.Symbol = LayoutRoot.Resources["DefaultBufferSymbol"] as ESRI.ArcGIS.Client.Symbols.Symbol;
graphicsLayer.Graphics.Add(graphic);
}
}
privatevoid GeometryService_Failed(object sender, TaskFailedEventArgs e)
{
MessageBox.Show("Geometry Service error: " + e.Error);
}
}
}
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 LocalBufferPoint
Inherits UserControl
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.BufferCompleted, AddressOf GeometryService_BufferCompleted
AddHandler geometryService.Failed, AddressOf GeometryService_Failed
MyMap.IsEnabled = TrueEndFunction
localGeometryService.StartAsync()
EndSubPrivateSub MyMap_MouseClick(sender AsObject, e As ESRI.ArcGIS.Client.Map.MouseEventArgs)
Dim graphicsLayer As GraphicsLayer = TryCast(MyMap.Layers("MyGraphicsLayer"), GraphicsLayer)
graphicsLayer.ClearGraphics()
e.MapPoint.SpatialReference = MyMap.SpatialReference
Dim graphic As Graphic = New ESRI.ArcGIS.Client.Graphic() With { _
.Geometry = e.MapPoint, _
.Symbol = TryCast(LayoutRoot.Resources("DefaultClickSymbol"), ESRI.ArcGIS.Client.Symbols.Symbol) _
}
graphic.SetZIndex(1)
graphicsLayer.Graphics.Add(graphic)
Dim bufferParams AsNew BufferParameters() With { _
.Unit = LinearUnit.Degree, _
.BufferSpatialReference = New SpatialReference(4326), _
.OutSpatialReference = MyMap.SpatialReference _
}
bufferParams.Features.Add(graphic)
bufferParams.Distances.Add(5)
geometryService.BufferAsync(bufferParams)
EndSubPrivateSub GeometryService_BufferCompleted(sender AsObject, args As GraphicsEventArgs)
Dim results As IList(Of Graphic) = args.Results
Dim graphicsLayer As GraphicsLayer = TryCast(MyMap.Layers("MyGraphicsLayer"), GraphicsLayer)
ForEach graphic As Graphic In results
graphic.Symbol = TryCast(LayoutRoot.Resources("DefaultBufferSymbol"), ESRI.ArcGIS.Client.Symbols.Symbol)
graphicsLayer.Graphics.Add(graphic)
NextEndSubPrivateSub GeometryService_Failed(sender AsObject, e As TaskFailedEventArgs)
MessageBox.Show("Geometry Service error: " & Convert.ToString(e.[Error]))
EndSubEndClassEndNamespace