Drive Times

Download Samples Repository

Description

This sample demonstrates use of the Geoprocessor to call a DriveTimes geoprocessing service. To use the sample, click a point in the map. Drive time polygons of 1, 2, and 3 minutes will be calculated and displayed on the map.

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

Sample Code

<UserControl x:Class="ArcGISRuntime.Samples.Desktop.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/runtime/2013">
    <Grid x:Name="layoutGrid">
        <Grid.Resources>
            <esri:SimpleMarkerSymbol x:Key="MarkerSymbol" Color="Black" Style="X" Size="8" />

            <esri:SimpleFillSymbol  x:Key="FillSymbol1" Color="#77FF9999">
                <esri:SimpleFillSymbol.Outline>
                    <esri:SimpleLineSymbol Color="#FFFF9999" Style="Solid" Width="2" />
                </esri:SimpleFillSymbol.Outline>
            </esri:SimpleFillSymbol>

            <esri:SimpleFillSymbol x:Key="FillSymbol2" Color="#77FFFF99">
                <esri:SimpleFillSymbol.Outline>
                    <esri:SimpleLineSymbol Color="#FFFFFF99" Style="Solid" Width="2" />
                </esri:SimpleFillSymbol.Outline>
            </esri:SimpleFillSymbol>

            <esri:SimpleFillSymbol x:Key="FillSymbol3" Color="#779999FF">
                <esri:SimpleFillSymbol.Outline>
                    <esri:SimpleLineSymbol Color="#FF9999FF" Style="Solid" Width="2" />
                </esri:SimpleFillSymbol.Outline>
            </esri:SimpleFillSymbol>
        </Grid.Resources>

        <esri:MapView x:Name="MyMapView"
					  MapViewTapped="MyMapView_MapViewTapped">
			<esri:Map InitialViewpoint="-122.5009,37.741,-122.3721,37.8089,4326">
                <esri:ArcGISTiledMapServiceLayer ID="StreetMapLayer"
                    ServiceUri="http://services.arcgisonline.com/ArcGIS/rest/services/ESRI_StreetMap_World_2D/MapServer" />
			</esri:Map>
			<esri:MapView.GraphicsOverlays>
				<esri:GraphicsOverlay ID="resultsOverlay"/>
				<esri:GraphicsOverlay ID="inputOverlay">
				<esri:GraphicsOverlay.Renderer>
					<esri:SimpleRenderer Symbol="{StaticResource MarkerSymbol}" />
					</esri:GraphicsOverlay.Renderer>
				</esri:GraphicsOverlay>
			</esri:MapView.GraphicsOverlays>
        </esri:MapView>

		<Border Background="White" BorderBrush="Black" BorderThickness="1"
				HorizontalAlignment="Right" VerticalAlignment="Top"
				Margin="30" Padding="20">
			<StackPanel>
                <TextBlock Text="Click on the map to set a location. Drive time areas of 1, 2, and 3 minutes will be displayed" 
                           TextWrapping="Wrap" HorizontalAlignment="Center" />
                
                <ProgressBar x:Name="progress" IsIndeterminate="True" Margin="12,16,12,0" Visibility="Collapsed" />
            </StackPanel>
        </Border>

    </Grid>
</UserControl>
using Esri.ArcGISRuntime.Controls;
using Esri.ArcGISRuntime.Layers;
using Esri.ArcGISRuntime.Symbology;
using Esri.ArcGISRuntime.Tasks.Geoprocessing;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Windows;
using System.Windows.Controls;

namespace ArcGISRuntime.Samples.Desktop
{
    /// <summary>
    /// This sample demonstrates use of the Geoprocessor to call a DriveTimes geoprocessing service. To use the sample, click a point in the map. Drive time polygons of 1, 2, and 3 minutes will be calculated and displayed on the map.
    /// </summary>
    /// <title>Drive Times</title>
    /// <category>Tasks</category>
    /// <subcategory>Geoprocessing</subcategory>
    public partial class DriveTimes : UserControl
    {
        private const string DriveTimeServiceUrl =
            "http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Network/ESRI_DriveTime_US/GPServer/CreateDriveTimePolygons";

        private GraphicsOverlay _resultsOverlay;
        private GraphicsOverlay _inputOverlay;
        private List<Symbol> _bufferSymbols;
        private Geoprocessor _gpTask;
        private CancellationTokenSource _cancellationTokenSource;


        /// <summary>Constructs Drive Times sample control</summary>
        public DriveTimes()
        {
            InitializeComponent();

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

            _bufferSymbols = new List<Symbol>()
            {
                layoutGrid.Resources["FillSymbol1"] as Symbol, 
                layoutGrid.Resources["FillSymbol2"] as Symbol, 
                layoutGrid.Resources["FillSymbol3"] as Symbol
            };

            _gpTask = new Geoprocessor(new Uri(DriveTimeServiceUrl));
        }

        // Use geoprocessor to call drive times gp service and display results
        private async void MyMapView_MapViewTapped(object sender, MapViewInputEventArgs e)
        {
            try
            {
               
                progress.Visibility = Visibility.Visible;

                // If the _gpTask has completed successfully (or before it is run the first time), the _cancellationTokenSource is set to null. 
                // If it has not completed and the map is clicked/tapped again, set the _cancellationTokenSource to cancel the initial task. 
                if (_cancellationTokenSource != null)
                {
                    _cancellationTokenSource.Cancel(); // Cancel previous operation
                }

                _cancellationTokenSource = new CancellationTokenSource();

                _inputOverlay.Graphics.Clear();
                _resultsOverlay.Graphics.Clear();

                _inputOverlay.Graphics.Add(new Graphic(e.Location));

                var parameter = new GPInputParameter();
                parameter.GPParameters.Add(new GPFeatureRecordSetLayer("Input_Location", e.Location));
                parameter.GPParameters.Add(new GPString("Drive_Times", "1 2 3"));

                var result = await _gpTask.ExecuteAsync(parameter, _cancellationTokenSource.Token);

                _cancellationTokenSource = null; // null out to show there are no pending operations

                if (result != null)
                {
                    var features = result.OutParameters.OfType<GPFeatureRecordSetLayer>().First().FeatureSet.Features;
                    _resultsOverlay.Graphics.AddRange(features.Select((fs, idx) => new Graphic(fs.Geometry, _bufferSymbols[idx])));
                }
            }
            catch (OperationCanceledException)
            {
                // Catch this exception because it is expected (when task cancellation is successful).
                // Catching it here prevents the message from being propagated to a MessageBox. 

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Sample Error");
            }
            finally
            {
                progress.Visibility = Visibility.Collapsed;
            }
        }
    }
}
Feedback on this topic?