Click or drag to resize

RouteTask Class

Class for route tasks (online and local).
Inheritance Hierarchy
SystemObject
  Esri.ArcGISRuntime.Tasks.NetworkAnalysisRouteTask

Namespace:  Esri.ArcGISRuntime.Tasks.NetworkAnalysis
Assembly:  Esri.ArcGISRuntime (in Esri.ArcGISRuntime.dll) Version: 100.5.0.0
Syntax
public sealed class RouteTask

The RouteTask type exposes the following members.

Properties
  NameDescription
Public propertyCredential
Gets or sets the security credential used to access a remote task
Public propertyRouteTaskInfo
Gets route task info. RouteTask must be loaded before accessing this property.
Public propertyTransportationNetworkDataset
Gets the transportation network dataset associated with the route task.
Public propertyUri
Gets the Uri of the route task.
Top
Methods
  NameDescription
Public methodStatic memberCode exampleCreateAsync(TransportationNetworkDataset)
Creates a route task from a transportation network dataset.
Public methodStatic memberCreateAsync(String, String)
Creates a route task from a local geodatabase.
Public methodStatic memberCode exampleCreateAsync(Uri, Credential)
Creates a route task from an online URI.
Public methodCode exampleCreateDefaultParametersAsync
Creates default parameters for the route task.
Public methodCreateParametersAsync(FeatureCollection) Obsolete.
Imports route parameters from feature collection and then adjusts imported parameters to current Route task.
Public methodCreateParametersAsync(PortalItem)
Imports route parameters from portal item and then adjusts imported parameters to current Route task.
Public methodStatic memberCreateRouteResultAsync(FeatureCollection)
Imports a route result from a Route Layer feature collection.
Public methodStatic memberCreateRouteResultAsync(PortalItem)
Imports route result from portal item.
Public methodCode exampleSolveRouteAsync
Solves a route with the specified parameters.
Top
Examples

WPF

Example Name: FindRoute

This sample demonstrates how to solve for the best route between two locations on the map and display driving directions between them.

Code example screen shot.

C#
// Copyright 2017 Esri.
// 
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
// You may obtain a copy of the License at: http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific
// language governing permissions and limitations under the License.

using Esri.ArcGISRuntime.Geometry;
using Esri.ArcGISRuntime.Mapping;
using Esri.ArcGISRuntime.Symbology;
using Esri.ArcGISRuntime.Tasks.NetworkAnalysis;
using Esri.ArcGISRuntime.UI;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Windows;

namespace ArcGISRuntime.WPF.Samples.FindRoute
{
    [ArcGISRuntime.Samples.Shared.Attributes.Sample(
        "Find a route",
        "Network Analysis",
        "This sample demonstrates how to solve for the best route between two locations on the map and display driving directions between them.",
        "")]
    public partial class FindRoute
    {
        // List of stops on the route ('from' and 'to')
        private List<Stop> _routeStops;

        // Graphics overlay to display stops and the route result
        private GraphicsOverlay _routeGraphicsOverlay;

        // URI for the San Diego route service
        private Uri _sanDiegoRouteServiceUri = new Uri("https://sampleserver6.arcgisonline.com/arcgis/rest/services/NetworkAnalysis/SanDiego/NAServer/Route");

        // URIs for picture marker images
        private Uri _checkedFlagIconUri = new Uri("https://static.arcgis.com/images/Symbols/Transportation/CheckeredFlag.png");

        private Uri _carIconUri = new Uri("https://static.arcgis.com/images/Symbols/Transportation/CarRedFront.png");

        public FindRoute()
        {
            InitializeComponent();

            // Create the map, graphics overlay, and the 'from' and 'to' locations for the route
            Initialize();
        }

        private void Initialize()
        {
            // Define the route stop locations (points)
            MapPoint fromPoint = new MapPoint(-117.15494348793044, 32.706506537686927, SpatialReferences.Wgs84);
            MapPoint toPoint = new MapPoint(-117.14905088669816, 32.735308180609138, SpatialReferences.Wgs84);

            // Create Stop objects with the points and add them to a list of stops
            Stop stop1 = new Stop(fromPoint);
            Stop stop2 = new Stop(toPoint);
            _routeStops = new List<Stop> {stop1, stop2};

            // Picture marker symbols: from = car, to = checkered flag
            PictureMarkerSymbol carSymbol = new PictureMarkerSymbol(_carIconUri);
            PictureMarkerSymbol flagSymbol = new PictureMarkerSymbol(_checkedFlagIconUri);

            // Add a slight offset (pixels) to the picture symbols.
            carSymbol.OffsetX = -carSymbol.Width / 2;
            carSymbol.OffsetY = -carSymbol.Height / 2;
            flagSymbol.OffsetX = -flagSymbol.Width / 2;
            flagSymbol.OffsetY = -flagSymbol.Height / 2;

            // Create graphics for the stops
            Graphic fromGraphic = new Graphic(fromPoint, carSymbol);
            Graphic toGraphic = new Graphic(toPoint, flagSymbol);

            // Create the graphics overlay and add the stop graphics
            _routeGraphicsOverlay = new GraphicsOverlay();
            _routeGraphicsOverlay.Graphics.Add(fromGraphic);
            _routeGraphicsOverlay.Graphics.Add(toGraphic);

            // Get an Envelope that covers the area of the stops (and a little more)
            Envelope routeStopsExtent = new Envelope(fromPoint, toPoint);
            EnvelopeBuilder envBuilder = new EnvelopeBuilder(routeStopsExtent);
            envBuilder.Expand(1.5);

            // Create a new viewpoint apply it to the map view when the spatial reference changes
            Viewpoint sanDiegoViewpoint = new Viewpoint(envBuilder.ToGeometry());
            MyMapView.SpatialReferenceChanged += (s, e) => MyMapView.SetViewpoint(sanDiegoViewpoint);

            // Add a new Map and the graphics overlay to the map view
            MyMapView.Map = new Map(Basemap.CreateStreetsVector());
            MyMapView.GraphicsOverlays.Add(_routeGraphicsOverlay);
        }

        private async void SolveRouteClick(object sender, System.Windows.RoutedEventArgs e)
        {
            try
            {
                // Create a new route task using the San Diego route service URI
                RouteTask solveRouteTask = await RouteTask.CreateAsync(_sanDiegoRouteServiceUri);

                // Get the default parameters from the route task (defined with the service)
                RouteParameters routeParams = await solveRouteTask.CreateDefaultParametersAsync();

                // Make some changes to the default parameters
                routeParams.ReturnStops = true;
                routeParams.ReturnDirections = true;

                // Set the list of route stops that were defined at startup
                routeParams.SetStops(_routeStops);

                // Solve for the best route between the stops and store the result
                RouteResult solveRouteResult = await solveRouteTask.SolveRouteAsync(routeParams);

                // Get the first (should be only) route from the result
                Route firstRoute = solveRouteResult.Routes.First();

                // Get the route geometry (polyline)
                Polyline routePolyline = firstRoute.RouteGeometry;

                // Create a thick purple line symbol for the route
                SimpleLineSymbol routeSymbol = new SimpleLineSymbol(SimpleLineSymbolStyle.Solid, Color.Purple, 8.0);

                // Create a new graphic for the route geometry and add it to the graphics overlay
                Graphic routeGraphic = new Graphic(routePolyline, routeSymbol);
                _routeGraphicsOverlay.Graphics.Add(routeGraphic);

                // Get a list of directions for the route and display it in the list box
                IReadOnlyList<DirectionManeuver> directionsList = firstRoute.DirectionManeuvers;
                DirectionsListBox.ItemsSource = directionsList;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), "Error");
            }
        }

        private void ResetClick(object sender, System.Windows.RoutedEventArgs e)
        {
            // Clear the list of directions
            DirectionsListBox.ItemsSource = null;

            // Remove the route graphic from the graphics overlay (only line graphic in the collection)
            int graphicsCount = _routeGraphicsOverlay.Graphics.Count;
            for (int i = graphicsCount; i > 0; i--)
            {
                // Get this graphic and see if it has line geometry
                Graphic g = _routeGraphicsOverlay.Graphics[i - 1];
                if (g.Geometry.GeometryType == GeometryType.Polyline)
                {
                    // Remove the graphic from the overlay
                    _routeGraphicsOverlay.Graphics.Remove(g);
                }
            }
        }
    }
}
XAML
<UserControl x:Class="ArcGISRuntime.WPF.Samples.FindRoute.FindRoute"
             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"
             xmlns:networkAnalysis="clr-namespace:Esri.ArcGISRuntime.Tasks.NetworkAnalysis;assembly=Esri.ArcGISRuntime">
    <Grid>
        <esri:MapView x:Name="MyMapView" />
        <Border Style="{StaticResource BorderStyle}">
            <StackPanel>
                <TextBlock Text="Route directions:"
                           Margin="0,0,0,2"
                           FontWeight="SemiBold" />
                <ListBox x:Name="DirectionsListBox"
                         HorizontalAlignment="Stretch"
                         ScrollViewer.HorizontalScrollBarVisibility="Disabled"
                         Height="300">
                    <ListBox.ItemTemplate>
                        <DataTemplate DataType="networkAnalysis:DirectionManeuver">
                            <TextBlock Text="{Binding DirectionText}" TextWrapping="Wrap" />
                        </DataTemplate>
                    </ListBox.ItemTemplate>
                </ListBox>
                <Button x:Name="SolveRouteButton"
                        HorizontalAlignment="Stretch"
                        Height="30"
                        Margin="0,5"
                        Content="Solve Route"
                        Click="SolveRouteClick" />
                <Button x:Name="ResetButton"
                        HorizontalAlignment="Stretch"
                        Height="30"
                        Content="Reset"
                        Click="ResetClick" />
            </StackPanel>
        </Border>
    </Grid>
</UserControl>
See Also
Additional Examples
Hyperlink to ExampleDescription
FindRouteThis sample demonstrates how to solve for the best route between two locations on the map and display driving directions between them.
MobileMapSearchAndRouteDisplay maps and use locators to enable search and routing offline using a Mobile Map Package.