This sample demonstrates use of a GeometryService to calculate a buffer. To use the sample, simply click a point on the map. The click point and a buffer of 5 miles around the point will be shown.
In the code-behind, a GeometryService is used to buffer the point. Note that, for accurate results, the BufferSpatialReference is set based on the area of interest.
<UserControlx:Class="ArcGISWPFSDK.BufferPoint"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"Extent="-10863035.970,3838021.340,-10744801.344,3887145.299"MouseClick="MyMap_MouseClick"><esri:ArcGISTiledMapServiceLayerID="StreetMapLayer"Url="http://services.arcgisonline.com/ArcGIS/rest/services/World_Street_Map//MapServer"/><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 miles 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;
namespace ArcGISWPFSDK
{
publicpartialclass BufferPoint : UserControl
{
public BufferPoint()
{
InitializeComponent();
}
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);
GeometryService geometryService =
new GeometryService("http://tasks.arcgisonline.com/ArcGIS/rest/services/Geometry/GeometryServer");
geometryService.BufferCompleted += GeometryService_BufferCompleted;
geometryService.Failed += GeometryService_Failed;
// If buffer spatial reference is GCS and unit is linear, geometry service will do geodesic buffering
BufferParameters bufferParams = new BufferParameters()
{
Unit = LinearUnit.StatuteMile,
BufferSpatialReference = new SpatialReference(4326),
OutSpatialReference = MyMap.SpatialReference
};
bufferParams.Features.Add(graphic);
bufferParams.Distances.Add(5);
geometryService.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
Namespace ArcGISWPFSDK
PartialPublicClass BufferPoint
Inherits UserControl
PublicSubNew()
InitializeComponent()
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 geometryService AsNew GeometryService("http://tasks.arcgisonline.com/ArcGIS/rest/services/Geometry/GeometryServer")
AddHandler geometryService.BufferCompleted, AddressOf GeometryService_BufferCompleted
AddHandler geometryService.Failed, AddressOf GeometryService_Failed
' If buffer spatial reference is GCS and unit is linear, geometry service will do geodesic bufferingDim bufferParams AsNew BufferParameters() With { _
.Unit = LinearUnit.StatuteMile, _
.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