This sample demonstrates use of the Geoprocessor to call a MessageInABottle geoprocessing service. To use the sample, specify the number of days and click a point in the ocean. The path of a bottle dropped at the click point over the specified number of days will be drawn on the map.
In the code-behind, a Geoprocessor is used to pass the number of days and the clicked point to the geoprocessing service. When the results are returned, they are added to a GraphicsLayer in the map.
<UserControlx:Class="ArcGISWPFSDK.MessageInABottle"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="StartMarkerSymbol"OffsetX="8"OffsetY="8"Source="/Assets/Images/x-16x16.png"/><esri:SimpleLineSymbolx:Key="PathLineSymbol"Color="Red"Width="3"Style="Dash"/><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"MouseClick="MyMap_MouseClick"WrapAround="True"><esri:Map.Layers><esri:ArcGISTiledMapServiceLayerID="TopoMapLayer"Url="http://services.arcgisonline.com/ArcGIS/rest/services/World_Topo_Map/MapServer"/><esri:GraphicsLayerID="MyGraphicsLayer"/></esri:Map.Layers></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"/><StackPanelOrientation="Vertical"Margin="20,20,20,20"HorizontalAlignment="Left"><TextBlockText="Message in a Bottle (Ocean Currents)"HorizontalAlignment="Center"FontWeight="Bold"Foreground="Black"/><TextBlockx:Name="InformationTextBlock"Text="Click on an ocean area in the map to track movement of currents."Width="200"TextAlignment="Left"TextWrapping="Wrap"Foreground="Black"/><StackPanelOrientation="Horizontal"HorizontalAlignment="Center"Margin="5,5,5,10"><TextBlockText="Days: "VerticalAlignment="Center"Foreground="Black"/><TextBoxx:Name="DaysTextBox"Text="365"Width="35"TextAlignment="Right"Margin="0,0,5,0"/></StackPanel></StackPanel></Grid></Grid></UserControl>
using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using ESRI.ArcGIS.Client;
using ESRI.ArcGIS.Client.Tasks;
using ESRI.ArcGIS.Client.Symbols;
namespace ArcGISWPFSDK
{
publicpartialclass MessageInABottle : UserControl
{
GraphicsLayer _graphicsLayer = null;
privatestatic ESRI.ArcGIS.Client.Projection.WebMercator _mercator =
new ESRI.ArcGIS.Client.Projection.WebMercator();
public MessageInABottle()
{
InitializeComponent();
_graphicsLayer = MyMap.Layers["MyGraphicsLayer"] as GraphicsLayer;
}
privatevoid MyMap_MouseClick(object sender, ESRI.ArcGIS.Client.Map.MouseEventArgs e)
{
Graphic graphic = new Graphic()
{
Symbol = LayoutRoot.Resources["StartMarkerSymbol"] as Symbol,
Geometry = e.MapPoint
};
_graphicsLayer.Graphics.Add(graphic);
Geoprocessor geoprocessorTask = new Geoprocessor("http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Specialty/ESRI_Currents_World/GPServer/MessageInABottle");
geoprocessorTask.ExecuteCompleted += GeoprocessorTask_ExecuteCompleted;
geoprocessorTask.Failed += GeoprocessorTask_Failed;
geoprocessorTask.OutputSpatialReference = MyMap.SpatialReference;
List<GPParameter> parameters = new List<GPParameter>();
parameters.Add(new GPFeatureRecordSetLayer("Input_Point", _mercator.ToGeographic(e.MapPoint)));
parameters.Add(new GPDouble("Days", Convert.ToDouble(DaysTextBox.Text)));
geoprocessorTask.ExecuteAsync(parameters);
}
privatevoid GeoprocessorTask_ExecuteCompleted(object sender, GPExecuteCompleteEventArgs e)
{
foreach (GPParameter gpParameter in e.Results.OutParameters)
{
if (gpParameter is GPFeatureRecordSetLayer)
{
GPFeatureRecordSetLayer gpLayer = gpParameter as GPFeatureRecordSetLayer;
foreach (Graphic graphic in gpLayer.FeatureSet.Features)
{
graphic.Symbol = LayoutRoot.Resources["PathLineSymbol"] as Symbol;
_graphicsLayer.Graphics.Add(graphic);
}
}
}
}
privatevoid GeoprocessorTask_Failed(object sender, TaskFailedEventArgs e)
{
MessageBox.Show("Geoprocessor service failed: " + e.Error);
}
}
}
Imports System.Collections.Generic
Imports System.Windows
Imports System.Windows.Controls
Imports ESRI.ArcGIS.Client
Imports ESRI.ArcGIS.Client.Tasks
Imports ESRI.ArcGIS.Client.Symbols
Namespace ArcGISWPFSDK
PartialPublicClass MessageInABottle
Inherits UserControl
Private _graphicsLayer As GraphicsLayer = NothingPrivateShared _mercator AsNew ESRI.ArcGIS.Client.Projection.WebMercator()
PublicSubNew()
InitializeComponent()
_graphicsLayer = TryCast(MyMap.Layers("MyGraphicsLayer"), GraphicsLayer)
EndSubPrivateSub MyMap_MouseClick(sender AsObject, e As ESRI.ArcGIS.Client.Map.MouseEventArgs)
Dim graphic AsNew Graphic() With { _
.Symbol = TryCast(LayoutRoot.Resources("StartMarkerSymbol"), Symbol), _
.Geometry = e.MapPoint _
}
_graphicsLayer.Graphics.Add(graphic)
Dim geoprocessorTask AsNew Geoprocessor("http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Specialty/ESRI_Currents_World/GPServer/MessageInABottle")
AddHandler geoprocessorTask.ExecuteCompleted, AddressOf GeoprocessorTask_ExecuteCompleted
AddHandler geoprocessorTask.Failed, AddressOf GeoprocessorTask_Failed
geoprocessorTask.OutputSpatialReference = MyMap.SpatialReference
Dim parameters AsNew List(Of GPParameter)()
parameters.Add(New GPFeatureRecordSetLayer("Input_Point", _mercator.ToGeographic(e.MapPoint)))
parameters.Add(New GPDouble("Days", Convert.ToDouble(DaysTextBox.Text)))
geoprocessorTask.ExecuteAsync(parameters)
EndSubPrivateSub GeoprocessorTask_ExecuteCompleted(sender AsObject, e As GPExecuteCompleteEventArgs)
ForEach gpParameter As GPParameter In e.Results.OutParameters
IfTypeOf gpParameter Is GPFeatureRecordSetLayer ThenDim gpLayer As GPFeatureRecordSetLayer = TryCast(gpParameter, GPFeatureRecordSetLayer)
ForEach graphic As Graphic In gpLayer.FeatureSet.Features
graphic.Symbol = TryCast(LayoutRoot.Resources("PathLineSymbol"), Symbol)
_graphicsLayer.Graphics.Add(graphic)
NextEndIfNextEndSubPrivateSub GeoprocessorTask_Failed(sender AsObject, e As TaskFailedEventArgs)
MessageBox.Show("Geoprocessor service failed: " & Convert.ToString(e.[Error]))
EndSubEndClassEndNamespace