This sample demonstrates use of the Geoprocessor to call a
DriveTimes geoprocessing service. To use the sample, simply click a
point in the map. Drive times polygons of 1, 2, and 3 minutes will
be calculated and shown on the map.
In the code-behind, a
Geoprocessor is used to pass the click point and drive time
parameters of 1, 2, and 3 minutes to the geoprocessing service.
When the results are returned, they are added to a GraphicsLayer in
the map.
<UserControlx:Class="ArcGISWPFSDK.DriveTimes"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><esri:PictureMarkerSymbolx:Key="DefaultMarkerSymbol"OffsetX="8"OffsetY="8"Source="/Assets/Images/car-red-16x16.png"/><esri:SimpleFillSymbolx:Key="FillSymbol1"Fill="#77FF9999"BorderBrush="#FFFF9999"BorderThickness="2"/><esri:SimpleFillSymbolx:Key="FillSymbol2"Fill="#77FFFF99"BorderBrush="#FFFFFF99"BorderThickness="2"/><esri:SimpleFillSymbolx:Key="FillSymbol3"Fill="#779999FF"BorderBrush="#FF9999FF"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"Background="White"Extent="-122.5009,37.741,-122.3721,37.8089"MouseClick="MyMap_MouseClick"><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 [Info]}"HorizontalAlignment="Left"Foreground="Black"/><TextBlockText="{Binding [LatLon]}"HorizontalAlignment="Left"Foreground="Black"/></StackPanel><BorderBorderBrush="Black"BorderThickness="1"/></Grid></esri:GraphicsLayer.MapTip></esri:GraphicsLayer></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="InformationText"Text="Click on map to set location. Drive time areas of 1, 2, and 3 minutes will be displayed"Width="200"Margin="30,20,30,25"HorizontalAlignment="Left"TextWrapping="Wrap"Foreground="Black"/></Grid></Grid></UserControl>
using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using ESRI.ArcGIS.Client;
using ESRI.ArcGIS.Client.Symbols;
using ESRI.ArcGIS.Client.Tasks;
namespace ArcGISWPFSDK
{
publicpartialclass DriveTimes : UserControl
{
Geoprocessor _geoprocessorTask;
public DriveTimes()
{
InitializeComponent();
_geoprocessorTask = new Geoprocessor("http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/" +
"Network/ESRI_DriveTime_US/GPServer/CreateDriveTimePolygons");
_geoprocessorTask.ExecuteCompleted += GeoprocessorTask_ExecuteCompleted;
_geoprocessorTask.Failed += GeoprocessorTask_Failed;
}
privatevoid MyMap_MouseClick(object sender, ESRI.ArcGIS.Client.Map.MouseEventArgs e)
{
_geoprocessorTask.CancelAsync();
GraphicsLayer graphicsLayer = MyMap.Layers["MyGraphicsLayer"] as GraphicsLayer;
graphicsLayer.ClearGraphics();
Graphic graphic = new Graphic()
{
Symbol = LayoutRoot.Resources["DefaultMarkerSymbol"] as Symbol,
Geometry = e.MapPoint,
};
graphic.Attributes.Add("Info", "Start location");
string latlon = String.Format("{0}, {1}", e.MapPoint.X, e.MapPoint.Y);
graphic.Attributes.Add("LatLon", latlon);
graphic.SetZIndex(1);
graphicsLayer.Graphics.Add(graphic);
List<GPParameter> parameters = new List<GPParameter>();
parameters.Add(new GPFeatureRecordSetLayer("Input_Location", e.MapPoint));
parameters.Add(new GPString("Drive_Times", "1 2 3"));
_geoprocessorTask.ExecuteAsync(parameters);
}
privatevoid GeoprocessorTask_ExecuteCompleted(object sender, ESRI.ArcGIS.Client.Tasks.GPExecuteCompleteEventArgs args)
{
GraphicsLayer graphicsLayer = MyMap.Layers["MyGraphicsLayer"] as GraphicsLayer;
foreach (GPParameter parameter in args.Results.OutParameters)
{
if (parameter is GPFeatureRecordSetLayer)
{
GPFeatureRecordSetLayer gpLayer = parameter as GPFeatureRecordSetLayer;
List<FillSymbol> bufferSymbols = new List<FillSymbol>(
new FillSymbol[] { LayoutRoot.Resources["FillSymbol1"] as FillSymbol, LayoutRoot.Resources["FillSymbol2"] as FillSymbol, LayoutRoot.Resources["FillSymbol3"] as FillSymbol });
int count = 0;
foreach (Graphic graphic in gpLayer.FeatureSet.Features)
{
graphic.Symbol = bufferSymbols[count];
graphic.Attributes.Add("Info", String.Format("{0} minute buffer ", 3 - count));
graphicsLayer.Graphics.Add(graphic);
count++;
}
}
}
}
privatevoid GeoprocessorTask_Failed(object sender, TaskFailedEventArgs e)
{
MessageBox.Show("Geoprocessing service failed: " + e.Error);
}
}
}
Imports System.Collections.Generic
Imports System.Windows
Imports System.Windows.Controls
Imports ESRI.ArcGIS.Client
Imports ESRI.ArcGIS.Client.Symbols
Imports ESRI.ArcGIS.Client.Tasks
Namespace ArcGISWPFSDK
PartialPublicClass DriveTimes
Inherits UserControl
Private _geoprocessorTask As Geoprocessor
PublicSubNew()
InitializeComponent()
_geoprocessorTask = New Geoprocessor("http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/" & "Network/ESRI_DriveTime_US/GPServer/CreateDriveTimePolygons")
AddHandler _geoprocessorTask.ExecuteCompleted, AddressOf GeoprocessorTask_ExecuteCompleted
AddHandler _geoprocessorTask.Failed, AddressOf GeoprocessorTask_Failed
EndSubPrivateSub MyMap_MouseClick(sender AsObject, e As ESRI.ArcGIS.Client.Map.MouseEventArgs)
_geoprocessorTask.CancelAsync()
Dim graphicsLayer As GraphicsLayer = TryCast(MyMap.Layers("MyGraphicsLayer"), GraphicsLayer)
graphicsLayer.ClearGraphics()
Dim graphic AsNew Graphic() With { _
.Symbol = TryCast(LayoutRoot.Resources("DefaultMarkerSymbol"), Symbol), _
.Geometry = e.MapPoint _
}
graphic.Attributes.Add("Info", "Start location")
Dim latlon AsString = [String].Format("{0}, {1}", e.MapPoint.X, e.MapPoint.Y)
graphic.Attributes.Add("LatLon", latlon)
graphic.SetZIndex(1)
graphicsLayer.Graphics.Add(graphic)
Dim parameters AsNew List(Of GPParameter)()
parameters.Add(New GPFeatureRecordSetLayer("Input_Location", e.MapPoint))
parameters.Add(New GPString("Drive_Times", "1 2 3"))
_geoprocessorTask.ExecuteAsync(parameters)
EndSubPrivateSub GeoprocessorTask_ExecuteCompleted(sender AsObject, args As ESRI.ArcGIS.Client.Tasks.GPExecuteCompleteEventArgs)
Dim graphicsLayer As GraphicsLayer = TryCast(MyMap.Layers("MyGraphicsLayer"), GraphicsLayer)
ForEach parameter As GPParameter In args.Results.OutParameters
IfTypeOf parameter Is GPFeatureRecordSetLayer ThenDim gpLayer As GPFeatureRecordSetLayer = TryCast(parameter, GPFeatureRecordSetLayer)
Dim bufferSymbols AsNew List(Of FillSymbol)(New FillSymbol() {TryCast(LayoutRoot.Resources("FillSymbol1"), FillSymbol), TryCast(LayoutRoot.Resources("FillSymbol2"), FillSymbol), TryCast(LayoutRoot.Resources("FillSymbol3"), FillSymbol)})
DimcountAsInteger = 0
ForEach graphic As Graphic In gpLayer.FeatureSet.Features
graphic.Symbol = bufferSymbols(count)
graphic.Attributes.Add("Info", [String].Format("{0} minute buffer ", 3 - count))
graphicsLayer.Graphics.Add(graphic)
count += 1
NextEndIfNextEndSubPrivateSub GeoprocessorTask_Failed(sender AsObject, e As TaskFailedEventArgs)
MessageBox.Show("Geoprocessing service failed: " & Convert.ToString(e.[Error]))
EndSubEndClassEndNamespace