This sample demonstrates using the ArcGIS Runtime SDK for WPF to use a geometry service to generate query geometry. To use the sample, simply click on the map. The click point will be buffered and States that intersect the buffer will be retrieved and drawn.
In the code-behind, a LocalGeometryService is used to generate the buffer from the click point. A QueryTask is then used to perform a query using the buffer geometry.
<UserControlx:Class="ArcGISWPFSDK.LocalBufferQuery"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"Background="White"><Grid.Resources><BooleanToVisibilityConverterx:Key="BooleanToVisibilityConverter"/><esri:PictureMarkerSymbolx:Key="DefaultMarkerSymbol"OffsetX="11"OffsetY="39"Source="/Assets/Images/i_pushpin.png"/><esri:SimpleFillSymbolx:Key="BufferSymbol"Fill="#66BB0000"BorderBrush="#88CC0000"BorderThickness="2"/><esri:SimpleFillSymbolx:Key="ParcelSymbol"Fill="#440000FF"BorderBrush="Blue"BorderThickness="2"/><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"WrapAround="True"Extent="-15000000,2000000,-7000000,8000000"Background="#FFE3E3E3"MinimumResolution="2445.98490512499"><esri:ArcGISLocalTiledLayerID="BaseMap"Path="..\\Data\\TPKs\\Topographic.tpk"/><esri:GraphicsLayerID="MyResultsGraphicsLayer"><esri:GraphicsLayer.MapTip><BorderBorderBrush="DarkGray"Background="Azure"CornerRadius="3"BorderThickness="1"Padding="3"><StackPanelOrientation="Vertical"><TextBlockText="State:"FontWeight="Bold"HorizontalAlignment="Left"VerticalAlignment="Center"Foreground="Black"/><TextBlockText="{Binding [STATE_NAME]}"HorizontalAlignment="Left"VerticalAlignment="Center"Foreground="Black"/></StackPanel></Border></esri:GraphicsLayer.MapTip></esri:GraphicsLayer><esri:GraphicsLayerID="MyGraphicsLayer"IsHitTestVisible="False"/></esri:Map><GridHorizontalAlignment="Right"VerticalAlignment="Top"Margin="0,15,15,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 will used to display States within 5 degrees of the location."Width="200"Margin="30,20,30,25"HorizontalAlignment="Left"TextWrapping="Wrap"Foreground="Black"/></Grid><ProgressBarx:Name="MyProgressBar"IsIndeterminate="True"VerticalAlignment="Bottom"Width="200"Height="20"Margin="10"Visibility="{Binding Path=IsBusy, Converter={StaticResource BooleanToVisibilityConverter}}"></ProgressBar></Grid></UserControl>
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;
using System.ComponentModel;
namespace ArcGISWPFSDK
{
publicpartialclass LocalBufferQuery : UserControl, INotifyPropertyChanged
{
publicevent PropertyChangedEventHandler PropertyChanged;
privatebool _isBusy = true;
publicbool IsBusy
{
get
{
return _isBusy;
}
set
{
_isBusy = value;
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs("IsBusy"));
}
}
}
QueryTask _queryTask;
Query _query;
GraphicsLayer _pointAndBufferGraphicsLayer;
GraphicsLayer _resultsGraphicsLayer;
GeometryService _geometryService;
LocalMapService _mapService;
public LocalBufferQuery()
{
InitializeComponent();
LocalGeometryService localGeometryService = new LocalGeometryService();
localGeometryService.StartCompleted += (sender, eventargs) =>
{
LocalGeometryService lgs = sender as LocalGeometryService;
_geometryService = new GeometryService();
_geometryService.Url = lgs.UrlGeometryService;
_geometryService.BufferCompleted += GeometryService_BufferCompleted;
_geometryService.Failed += GeometryService_Failed;
MyMap.MouseClick += MyMap_MouseClick;
};
localGeometryService.StartAsync();
LocalMapService.GetServiceAsync("..\\Data\\MPKS\\USCitiesStates.mpk", (localMapService) =>
{
_mapService = localMapService;
_queryTask = new QueryTask();
_queryTask.Url = _mapService.UrlMapService + "/2";
_queryTask.ExecuteCompleted += QueryTask_ExecuteCompleted;
_queryTask.Failed += QueryTask_Failed;
DataContext = this;
IsBusy = false;
});
_pointAndBufferGraphicsLayer = MyMap.Layers["MyGraphicsLayer"] as GraphicsLayer;
_resultsGraphicsLayer = MyMap.Layers["MyResultsGraphicsLayer"] as GraphicsLayer;
}
privatevoid MyMap_MouseClick(object sender, ESRI.ArcGIS.Client.Map.MouseEventArgs e)
{
_geometryService.CancelAsync();
Graphic clickGraphic = new Graphic();
clickGraphic.Symbol = LayoutRoot.Resources["DefaultMarkerSymbol"] as ESRI.ArcGIS.Client.Symbols.Symbol;
clickGraphic.Geometry = e.MapPoint;
clickGraphic.Geometry.SpatialReference = MyMap.SpatialReference;
_pointAndBufferGraphicsLayer.ClearGraphics();
_resultsGraphicsLayer.ClearGraphics();
clickGraphic.SetZIndex(2);
_pointAndBufferGraphicsLayer.Graphics.Add(clickGraphic);
ESRI.ArcGIS.Client.Tasks.BufferParameters bufferParams = new ESRI.ArcGIS.Client.Tasks.BufferParameters()
{
BufferSpatialReference = new SpatialReference(4326),
OutSpatialReference = MyMap.SpatialReference,
Unit = LinearUnit.Degree,
};
bufferParams.Distances.Add(5);
bufferParams.Features.Add(clickGraphic);
_geometryService.BufferAsync(bufferParams);
}
void GeometryService_BufferCompleted(object sender, GraphicsEventArgs args)
{
Graphic bufferGraphic = new Graphic();
bufferGraphic.Geometry = args.Results[0].Geometry;
bufferGraphic.Symbol = LayoutRoot.Resources["BufferSymbol"] as ESRI.ArcGIS.Client.Symbols.Symbol;
bufferGraphic.SetZIndex(1);
_pointAndBufferGraphicsLayer.Graphics.Add(bufferGraphic);
_query = new ESRI.ArcGIS.Client.Tasks.Query();
_query.ReturnGeometry = true;
_query.OutSpatialReference = MyMap.SpatialReference;
_query.Geometry = bufferGraphic.Geometry;
_query.OutFields.Add("*");
_queryTask.ExecuteAsync(_query);
}
privatevoid QueryTask_ExecuteCompleted(object sender, QueryEventArgs args)
{
if (args.FeatureSet.Features.Count < 1)
{
MessageBox.Show("No features found");
return;
}
foreach (Graphic selectedGraphic in args.FeatureSet.Features)
{
selectedGraphic.Symbol = LayoutRoot.Resources["ParcelSymbol"] as ESRI.ArcGIS.Client.Symbols.Symbol;
_resultsGraphicsLayer.Graphics.Add(selectedGraphic);
}
}
privatevoid GeometryService_Failed(object sender, TaskFailedEventArgs args)
{
MessageBox.Show("Geometry service failed: " + args.Error);
}
privatevoid QueryTask_Failed(object sender, TaskFailedEventArgs args)
{
MessageBox.Show("Query failed: " + args.Error);
}
}
}
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
Imports System.ComponentModel
Namespace ArcGISWPFSDK
PartialPublicClass LocalBufferQuery
Inherits UserControl
Implements INotifyPropertyChanged
PublicEvent PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged
Private _isBusy AsBoolean = TruePublicProperty IsBusy() AsBooleanGetReturn _isBusy
EndGetSet(value AsBoolean)
_isBusy = value
RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs("IsBusy"))
EndSetEndPropertyPrivate _queryTask As QueryTask
Private _query As Query
Private _pointAndBufferGraphicsLayer As GraphicsLayer
Private _resultsGraphicsLayer As GraphicsLayer
Private _geometryService As GeometryService
Private _mapService As LocalMapService
PublicSubNew()
InitializeComponent()
Dim localGeometryService AsNew LocalGeometryService()
AddHandler localGeometryService.StartCompleted, AddressOf localGeometryService_StartCompleted
localGeometryService.StartAsync()
LocalMapService.GetServiceAsync("..\Data\MPKS\USCitiesStates.mpk", Function(localMapService__1)
_mapService = localMapService__1
_queryTask = New QueryTask()
_queryTask.Url = _mapService.UrlMapService & "/2"AddHandler _queryTask.ExecuteCompleted, AddressOf QueryTask_ExecuteCompleted
AddHandler _queryTask.Failed, AddressOf QueryTask_Failed
DataContext = Me
IsBusy = FalseEndFunction)
_pointAndBufferGraphicsLayer = TryCast(MyMap.Layers("MyGraphicsLayer"), GraphicsLayer)
_resultsGraphicsLayer = TryCast(MyMap.Layers("MyResultsGraphicsLayer"), GraphicsLayer)
EndSubPrivateSub MyMap_MouseClick(sender AsObject, e As ESRI.ArcGIS.Client.Map.MouseEventArgs)
_geometryService.CancelAsync()
Dim clickGraphic AsNew Graphic()
clickGraphic.Symbol = TryCast(LayoutRoot.Resources("DefaultMarkerSymbol"), ESRI.ArcGIS.Client.Symbols.Symbol)
clickGraphic.Geometry = e.MapPoint
clickGraphic.Geometry.SpatialReference = MyMap.SpatialReference
_pointAndBufferGraphicsLayer.ClearGraphics()
_resultsGraphicsLayer.ClearGraphics()
clickGraphic.SetZIndex(2)
_pointAndBufferGraphicsLayer.Graphics.Add(clickGraphic)
Dim bufferParams AsNew ESRI.ArcGIS.Client.Tasks.BufferParameters() With { _
.BufferSpatialReference = New SpatialReference(4326), _
.OutSpatialReference = MyMap.SpatialReference, _
.Unit = LinearUnit.Degree _
}
bufferParams.Distances.Add(5)
bufferParams.Features.Add(clickGraphic)
_geometryService.BufferAsync(bufferParams)
EndSubPrivateSub GeometryService_BufferCompleted(sender AsObject, args As GraphicsEventArgs)
Dim bufferGraphic AsNew Graphic()
bufferGraphic.Geometry = args.Results(0).Geometry
bufferGraphic.Symbol = TryCast(LayoutRoot.Resources("BufferSymbol"), ESRI.ArcGIS.Client.Symbols.Symbol)
bufferGraphic.SetZIndex(1)
_pointAndBufferGraphicsLayer.Graphics.Add(bufferGraphic)
_query = New ESRI.ArcGIS.Client.Tasks.Query()
_query.ReturnGeometry = True
_query.OutSpatialReference = MyMap.SpatialReference
_query.Geometry = bufferGraphic.Geometry
_query.OutFields.Add("*")
_queryTask.ExecuteAsync(_query)
EndSubPrivateSub QueryTask_ExecuteCompleted(sender AsObject, args As QueryEventArgs)
If args.FeatureSet.Features.Count < 1 Then
MessageBox.Show("No features found")
ReturnEndIfForEach selectedGraphic As Graphic In args.FeatureSet.Features
selectedGraphic.Symbol = TryCast(LayoutRoot.Resources("ParcelSymbol"), ESRI.ArcGIS.Client.Symbols.Symbol)
_resultsGraphicsLayer.Graphics.Add(selectedGraphic)
NextEndSubPrivateSub GeometryService_Failed(sender AsObject, args As TaskFailedEventArgs)
MessageBox.Show("Geometry service failed: " & Convert.ToString(args.[Error]))
EndSubPrivateSub QueryTask_Failed(sender AsObject, args As TaskFailedEventArgs)
MessageBox.Show("Query failed: " & Convert.ToString(args.[Error]))
EndSubPrivateSub localGeometryService_StartCompleted(sender AsObject, eventargs As ComponentModel.AsyncCompletedEventArgs)
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
AddHandler MyMap.MouseClick, AddressOf MyMap_MouseClick
EndSubEndClassEndNamespace