Message in a Bottle

Download Samples Repository

Description

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.

"Desktop" "Store" "Phone" Available for Desktop, Store, Phone

Sample Code

<UserControl x:Class="ArcGISRuntime.Samples.Desktop.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/runtime/2013">
    <Grid x:Name="LayoutGrid">
        <Grid.Resources>
            <esri:SimpleMarkerSymbol x:Key="StartMarkerSymbol" Color="Black" Size="6" Style="X" />
            <esri:SimpleRenderer x:Key="PointRenderer" Symbol="{StaticResource StartMarkerSymbol}" />

            <esri:SimpleLineSymbol x:Key="PathLineSymbol" Color="Red" Width="3" Style="DashDotDot" />
            <esri:SimpleRenderer x:Key="LineRenderer" Symbol="{StaticResource PathLineSymbol}" />
        </Grid.Resources>

        <esri:MapView x:Name="MyMapView" WrapAround="True" 
					  MapViewTapped="MyMapView_MapViewTapped">
            <esri:Map>
				<esri:ArcGISTiledMapServiceLayer ID="TopoMapLayer"
                    ServiceUri="http://services.arcgisonline.com/ArcGIS/rest/services/World_Topo_Map/MapServer" />
			</esri:Map>
			<esri:MapView.GraphicsOverlays>
				<esri:GraphicsOverlay ID="resultsOverlay" Renderer="{StaticResource LineRenderer}"/>
				<esri:GraphicsOverlay ID="inputOverlay" Renderer="{StaticResource PointRenderer}"/>
			</esri:MapView.GraphicsOverlays>
        </esri:MapView>

		<Border Background="White" BorderBrush="Black" BorderThickness="1"
				HorizontalAlignment="Right" VerticalAlignment="Top"
				Margin="30" Padding="20">
			<Border.Effect>
				<DropShadowEffect/>
			</Border.Effect>
			<StackPanel>
                <TextBlock Text="Message in a Bottle (Ocean Currents)"
                           HorizontalAlignment="Center" FontWeight="Bold" />
                
                <TextBlock x:Name="InformationTextBlock" Text="Click on an ocean area in the map to track movement of currents."
                           Width="200" TextAlignment="Left" TextWrapping="Wrap" Margin="8" />
                
                <StackPanel Orientation="Horizontal" HorizontalAlignment="Center" Margin="0,8,0,0">
                    <TextBlock Text="Days:" VerticalAlignment="Center" />
                    <TextBox x:Name="DaysTextBox" Text="365" Width="35" TextAlignment="Right" Margin="8,0,0,0" />
                </StackPanel>
                
                <ProgressBar x:Name="Progress" IsIndeterminate="True" Margin="12,16,12,0" Visibility="Collapsed" />
            </StackPanel>
        </Border>
    </Grid>
</UserControl>
using Esri.ArcGISRuntime.Controls;
using Esri.ArcGISRuntime.Geometry;
using Esri.ArcGISRuntime.Layers;
using Esri.ArcGISRuntime.Tasks.Geoprocessing;
using System;
using System.Linq;
using System.Windows;
using System.Windows.Controls;

namespace ArcGISRuntime.Samples.Desktop
{
    /// <summary>
    /// 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.
    /// </summary>
    /// <title>Message in a Bottle</title>
	/// <category>Tasks</category>
	/// <subcategory>Geoprocessing</subcategory>
	public partial class MessageInABottle : UserControl
    {
        private const string MessageInABottleServiceUrl =
            "http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Specialty/ESRI_Currents_World/GPServer/MessageInABottle";

		private GraphicsOverlay _inputOverlay;
		private GraphicsOverlay _resultsOverlay;

        /// <summary>Construct Message In A Bottle sample control</summary>
        public MessageInABottle()
        {
            InitializeComponent();

			_inputOverlay = MyMapView.GraphicsOverlays["inputOverlay"];
			_resultsOverlay = MyMapView.GraphicsOverlays["resultsOverlay"];
        }

        // Begin geoprocessing with a user tap on the map
        private async void MyMapView_MapViewTapped(object sender, Esri.ArcGISRuntime.Controls.MapViewInputEventArgs e)
        {
            try
            {
                Progress.Visibility = Visibility.Visible;

				_resultsOverlay.Graphics.Clear();
                _inputOverlay.Graphics.Clear();
				_inputOverlay.Graphics.Add(new Graphic() { Geometry = e.Location });

                Geoprocessor geoprocessorTask = new Geoprocessor(new Uri(MessageInABottleServiceUrl));

                var parameter = new GPInputParameter() { OutSpatialReference = MyMapView.SpatialReference };
				var ptNorm = GeometryEngine.NormalizeCentralMeridian(e.Location);
                var ptGeographic = GeometryEngine.Project(ptNorm, SpatialReferences.Wgs84) as MapPoint;

                parameter.GPParameters.Add(new GPFeatureRecordSetLayer("Input_Point", ptGeographic));
                parameter.GPParameters.Add(new GPDouble("Days", Convert.ToDouble(DaysTextBox.Text)));

                var result = await geoprocessorTask.ExecuteAsync(parameter);

                foreach (GPParameter gpParameter in result.OutParameters)
                {
                    if (gpParameter is GPFeatureRecordSetLayer)
                    {
                        GPFeatureRecordSetLayer gpLayer = gpParameter as GPFeatureRecordSetLayer;
						_resultsOverlay.Graphics.AddRange(gpLayer.FeatureSet.Features.OfType<Graphic>());
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Geoprocessor service failed: " + ex.Message, "Sample Error");
            }
            finally
            {
                Progress.Visibility = Visibility.Collapsed;
            }
        }
    }
}
Feedback on this topic?