Using the routing service A routing service is a service that uses network analysis and streets data to calculate the most effective path and turn-by-turn directions on a street network, optimize fleet routing and deliveries, find the closest facilities, calculate service areas, and more. It is hosted by Esri as the ArcGIS Routing service and can also be hosted in ArcGIS Enterprise. Learn more , your own route services A service, also known as an ArcGIS service, is software that supports an ArcGIS REST API and provides geospatial functionality or data. A service can be hosted by Esri or in ArcGIS Enterprise. Learn more , or a route dataset stored on the device A device is nearly any kind of computer, including desktops, laptops, mobile phones, and tablets. Learn more , you can find routes A route is a polyline that defines the best path between two or more points in a street network. Learn more , directions, and perform advanced analyses on street networks. You can solve network problems such as finding the closest emergency vehicle or facility, identifying a service area A service area is a polygon representing the distance that can be reached within a specified length of time while traveling in a street network. Learn more around a location A location is a position or region (point, line, or polygon) on the earth's surface. Learn more , or servicing a set of orders with a fleet of vehicles.

Your routing app can do things like:

  • Calculate point-to-point and multi- stop A stop is a single point along a route: it can be the origin, an intermediate stop, or destination. Learn more routes A route is a polyline that defines the best path between two or more points in a street network. Learn more
  • Optimize the results to find the shortest or the fastest route
  • Find the best sequence between stops A stop is a single point along a route: it can be the origin, an intermediate stop, or destination. Learn more
  • Create routes based on the travel mode A travel mode is the means of transportation, such as walking or driving, that defines how you can travel in a street network. Learn more (driving, cycling, walking, and so on)
  • Avoid restricted areas and maneuvers
  • Specify time windows of arrival and departure for each stop A stop is a single point along a route: it can be the origin, an intermediate stop, or destination. Learn more
  • Generate driving directions in multiple languages

What is routing?

Routing is the process of finding the best path between two or more stops A stop is a single point along a route: it can be the origin, an intermediate stop, or destination. Learn more in a street network. A common use case is to find a route A route is a polyline that defines the best path between two or more points in a street network. Learn more between two stops, from an origin An origin is a point that defines the start of a route. Learn more to a destination A destination is a point that defines the final stop in a route. Learn more . Routing takes into consideration many different attributes Attributes are fields and values for a single feature or non-spatial record. They are typically stored in a database or service such as a feature service. Learn more in the street network such as speed limit, number of lanes, and time of day. Routing can also incorporate real-time data such as road conditions, accidents, street closures, and other restrictions to travel.

In addition to the routing described above, functionality is provided to analyze street networks. This includes the ability to create service areas A service area is a polygon representing the distance that can be reached within a specified length of time while traveling in a street network. Learn more (to find customers within a certain drive time, for example) or locate a closest facility (nearest hospital to an accident, for example).

Directions

If requested, directions are returned for each route A route is a polyline that defines the best path between two or more points in a street network. Learn more in the results. Directions provide specific instructions for traveling the route. These are often referred to as “driving directions”, but depending on the travel mode A travel mode is the means of transportation, such as walking or driving, that defines how you can travel in a street network. Learn more , could describe walking or cycling to the destination A destination is a point that defines the final stop in a route. Learn more . Depending on the services A service, also known as an ArcGIS service, is software that supports an ArcGIS REST API and provides geospatial functionality or data. A service can be hosted by Esri or in ArcGIS Enterprise. Learn more configuration, directions can be returned in more than one language. Supported languages for a service are available in the service metadata.

Directions are composed of maneuvers. Each maneuver contains properties such as the direction text, which contains instructions that describe travel through a section of a route A route is a polyline that defines the best path between two or more points in a street network. Learn more . A direction maneuver is further broken down into maneuver messages that give details about a specific maneuver.

If supported by the service A service, also known as an ArcGIS service, is software that supports an ArcGIS REST API and provides geospatial functionality or data. A service can be hosted by Esri or in ArcGIS Enterprise. Learn more , direction maneuver’s can include from level and to level values to define things like floor levels inside a building. For example, a user may want to see just one level of a walking route A route is a polyline that defines the best path between two or more points in a street network. Learn more that spans multiple floors.

The navigation API allows you to further enhance the routing experience using the current device location A location is a position or region (point, line, or polygon) on the earth's surface. Learn more to track progress and provide navigation instructions (maneuvers) as the user travels the route A route is a polyline that defines the best path between two or more points in a street network. Learn more . You can integrate driving directions with your device’s text-to-speech capability and automatically recalculate a new route when the user leaves the current one.

The route tracker object provides the following functionality using the current device location A location is a position or region (point, line, or polygon) on the earth's surface. Learn more and an appropriate route A route is a polyline that defines the best path between two or more points in a street network. Learn more result:

  • Progress information relative to the next stop A stop is a single point along a route: it can be the origin, an intermediate stop, or destination. Learn more , to the next maneuver, or along the entire route
  • Guidance for navigation as it’s needed (when approaching a maneuver, for example)
  • Automatic recalculation of a route A route is a polyline that defines the best path between two or more points in a street network. Learn more if the device location A location is a position or region (point, line, or polygon) on the earth's surface. Learn more goes off route

How routing works

Online and local routing both rely on a transportation network to model travel. These networks are created from features A feature is a single record, also known as a row, that represents a real-world entity. It typically contains a geometry (point, multipoint, polyline, or polygon) and attributes but it can also contain just attributes. Learn more (lines and points) that represent roads, bridges, tunnels, bike paths, train tracks, and other entities in the network. The geometric intersections of features help to define connectivity between the network entities they represent. The connectivity of the transportation network, along with resistance to travel (speed limit, for example) and other network properties, is analyzed to solve routing problems.

Network Analyst services A service, also known as an ArcGIS service, is software that supports an ArcGIS REST API and provides geospatial functionality or data. A service can be hosted by Esri or in ArcGIS Enterprise. Learn more are hosted in ArcGIS or can be published on your own ArcGIS servers. These services provide a REST API for clients such as mobile and Web applications. For more information about publishing routing services with ArcGIS Enterprise ArcGIS Enterprise is a GIS mapping, analytics, data hosting, and content management product that can be hosted on-premise or in a cloud infrastructure. It includes software, applications, tools, APIs, and services for users and developers. Learn more , see Publish routing services.

You can create a transportation network using ArcGIS Pro ArcGIS Pro is a professional desktop GIS application that can explore, visualize, analyze, and manage 2D and 3D data. Learn more and deploy it to your device using either a mobile map package A mobile map package (MMPK) is a standalone file that contains one or more map definitions, including the basemap layers, data layers, layer styles, and pop-up styles for use in offline applications built with ArcGIS Maps SDKs for Native Apps. Learn more file (.mmpk), a mobile scene package A mobile scene package (MSPK) is a standalone file that contains one or more scene definitions, including the basemap layers, elevation layers, data layers, layer styles, and pop-up styles for use in offline applications built with ArcGIS Maps SDKs for Native Apps. Learn more file (.mspk), or a mobile geodatabase A mobile geodatabase (.geodatabase) is a spatial data storage format in a single file on disk that can store, query, and manage spatial and nonspatial data. In applications built with the ArcGIS Maps SDKs for Native Apps, mobile geodatabases can be used in offline workflows when taking maps and features offline from services or in desktop-based scenarios as standalone mobile geodatabases from ArcGIS Pro. Learn more file. The code to work with a local transportation network (stored directly on the device A device is nearly any kind of computer, including desktops, laptops, mobile phones, and tablets. Learn more ) is identical to the code for using a routing service A service, also known as an ArcGIS service, is software that supports an ArcGIS REST API and provides geospatial functionality or data. A service can be hosted by Esri or in ArcGIS Enterprise. Learn more .

If you’d like a ready-to-use and regularly updated network dataset (and locator A locator is an ArcGIS dataset that stores address information and the rules for translating descriptions of places (such as street addresses or place names) into spatial data that can be displayed on a map. Learn more ) for your area of interest, you can license StreetMap Premium data (in mobile map package format). For details, see Add StreetMap Premium data.

Route task

A route task is a network analysis task that is executed asynchronously Asynchronous is a style of programming that involves executing multiple tasks simultaneously rather than sequentially. Learn more . It returns results with details about a route A route is a polyline that defines the best path between two or more points in a street network. Learn more that visits two or more stops A stop is a single point along a route: it can be the origin, an intermediate stop, or destination. Learn more ( locations A location is a position or region (point, line, or polygon) on the earth's surface. Learn more ) within a transportation network. This operation is sometimes referred to as solving a route A route is a polyline that defines the best path between two or more points in a street network. Learn more .

The RouteTask refers to the local transportation network dataset or online service. If solves a Route using the configured RouteParameters and reports the RouteResult.

/// Initializes the route task with the online routing service.
let routeTask = RouteTask(url: URL(string: "https://route-api.arcgis.com/arcgis/rest/services/World/Route/NAServer/Route_World")!)

Route parameters

Route task parameters specify how a route A route is a polyline that defines the best path between two or more points in a street network. Learn more should be found. There are many parameters that let you customize how the route is determined. Parameters include stops A stop is a single point along a route: it can be the origin, an intermediate stop, or destination. Learn more and barriers A barrier is a geometric feature that alters which streets a route can traverse in a street network. Learn more . Other parameters include travel mode A travel mode is the means of transportation, such as walking or driving, that defines how you can travel in a street network. Learn more , whether stops map be reordered to find an optimized route An optimized route is the lowest cost path that visits all stops specified by the user in the most efficient order, regardless of the order in which they are provided. Learn more , the units to be used in the turn-by-turn directions (miles or kilometers, for example), and so on.

To initially create RouteParameters, call RouteTask.makeDefaultParameters() to retrieve the default routing parameters defined for the service. You can then change individual route parameters as needed before executing the RouteTask. The service’s default parameters typically support the most common use case anticipated for that service. Different services can have different defaults.

// Retrieves the default route parameters from the service.
let parameters = try await routeTask.makeDefaultParameters()
// Sets relevant parameters. For example, returns driving directions in Spanish.
parameters.returnsDirections = true
parameters.directionsLanguage = "es"
// Adds stops.
parameters.setStops(stops)

Route results

The results returned from finding a route A route is a polyline that defines the best path between two or more points in a street network. Learn more contain a collection of routes. Each route is represented by a polyline A polyline is a type of geometry containing ordered point coordinates and a spatial reference. Learn more geometry and has information such as its total length and travel time. Depending on how you configured your route parameters, the results may also contain barriers A barrier is a geometric feature that alters which streets a route can traverse in a street network. Learn more , stops A stop is a single point along a route: it can be the origin, an intermediate stop, or destination. Learn more , and driving directions. Typically, the route A route is a polyline that defines the best path between two or more points in a street network. Learn more , stops, and barriers are displayed as graphics A graphic is a visual element composed of a geometry, symbol, and attributes that is displayed on a map or scene. Learn more in the map A map is a collection of layers that are displayed in 2D. It is typically composed of a basemap layer and data layers. Learn more . Directions can be shown in a list or other UI element in the app.

// Solves the route using the given parameters.
let routeResult = try await routeTask.solveRoute(using: parameters)
guard let route = routeResult.routes.first else { return }
// Creates a route symbol to display on the route graphics overlay.
let routeSymbol = SimpleLineSymbol(style: .solid, color: .yellow, width: 5)
// Shows the resulting route on the map.
let routeGraphic = Graphic(geometry: route.geometry, symbol: routeSymbol)
routeGraphicsOverlay.addGraphic(routeGraphic)
// Displays the driving directions
route.directionManeuvers.forEach { maneuver in
print(maneuver.text, separator: "\n")
}

Examples

Find a route and directions

Use the routing service A routing service is a service that uses network analysis and streets data to calculate the most effective path and turn-by-turn directions on a street network, optimize fleet routing and deliveries, find the closest facilities, calculate service areas, and more. It is hosted by Esri as the ArcGIS Routing service and can also be hosted in ArcGIS Enterprise. Learn more to find a route A route is a polyline that defines the best path between two or more points in a street network. Learn more between a set of stops A stop is a single point along a route: it can be the origin, an intermediate stop, or destination. Learn more . The result will account for current traffic conditions and provide driving directions in Spanish.

To find a route, you need to define at least two stops to visit. The default travel mode A travel mode is the means of transportation, such as walking or driving, that defines how you can travel in a street network. Learn more is driving, but you can use walk or trucking travel modes as well. It is always good to provide the start time to get the best results (to consider the appropriate traffic conditions, for example). In this example, the start time is set with the current time which indicates to the service that the route should use the current traffic conditions. The directions language is set to “es” to generate driving directions in Spanish.

The result contains a set of ordered stops A stop is a single point along a route: it can be the origin, an intermediate stop, or destination. Learn more to visit, the route segments, and turn-by-turn text directions.

75 collapsed lines
import ArcGIS
import SwiftUI
private class Model: ObservableObject {
let map: Map = {
let map = Map(basemapStyle: .arcGISNavigation)
map.initialViewpoint =
Viewpoint(
latitude: 45.526201,
longitude: -122.65,
scale: 144447.638572
)
return map
}()
/// A graphic to symbolize the start point of the route.
let originGraphic: Graphic = {
let symbol = SimpleMarkerSymbol(style: .circle, color: .white, size: 12)
symbol.outline = SimpleLineSymbol(style: .solid, color: .black, width: 2)
let graphic = Graphic(geometry: nil, symbol: symbol)
return graphic
}()
/// A graphic to symbolize a midway stop of the route.
let stopGraphic: Graphic = {
let symbol = SimpleMarkerSymbol(style: .circle, color: .white, size: 8)
symbol.outline = SimpleLineSymbol(style: .solid, color: .black, width: 2)
let graphic = Graphic(geometry: nil, symbol: symbol)
return graphic
}()
/// A graphic to symbolize the end point of the route.
let destinationGraphic: Graphic = {
let symbol = SimpleMarkerSymbol(style: .circle, color: .black, size: 12)
symbol.outline = SimpleLineSymbol(style: .solid, color: .white, width: 2)
let graphic = Graphic(geometry: nil, symbol: symbol)
return graphic
}()
/// A graphic to symbolize the route polyline.
let routeGraphic: Graphic = {
let symbol = SimpleLineSymbol(style: .solid, color: .systemBlue, width: 4)
let graphic = Graphic(geometry: nil, symbol: symbol)
return graphic
}()
/// A graphics overlay to display the route.
let routeGraphicsOverlay = GraphicsOverlay()
/// The route task to solve the route between stops, using the online routing service.
let routeTask = RouteTask(url: URL(string: "https://route-api.arcgis.com/arcgis/rest/services/World/Route/NAServer/Route_World")!)
/// Stops to demo route solving.
let demoStops = [
Stop(point: Point(x: -122.690176, y: 45.522054, spatialReference: .wgs84)), // origin
Stop(point: Point(x: -122.614995, y: 45.526201, spatialReference: .wgs84)), // stop
Stop(point: Point(x: -122.68782, y: 45.51238, spatialReference: .wgs84)) // destination
]
/// Sets the geometries and adds graphics to the graphics overlay.
func makeGraphics() {
originGraphic.geometry = demoStops[0].geometry
stopGraphic.geometry = demoStops[1].geometry
destinationGraphic.geometry = demoStops[2].geometry
// Adds the graphic to the graphics overlay.
routeGraphicsOverlay.addGraphics([
originGraphic,
stopGraphic,
destinationGraphic,
routeGraphic
])
}
func solveRoute(stops: [Stop]) async throws {
let parameters = try await routeTask.makeDefaultParameters()
// Sets up additional route parameters.
parameters.setStops(stops)
parameters.returnsDirections = true
parameters.directionsLanguage = "es"
// Solves the route.
let routeResult = try await routeTask.solveRoute(using: parameters)
// Displays route and print directions.
guard let solvedRoute = routeResult.routes.first else { return }
routeGraphic.geometry = solvedRoute.geometry
print(solvedRoute.directionManeuvers.map { $0.text }.joined(separator: "\n"))
21 collapsed lines
}
}
struct RouteExampleView: View {
// Creates a `StateObject` of the `Model` class.
@StateObject private var model = Model()
var body: some View {
// Creates a map view with the model's map and graphics overlay.
MapView(map: model.map, graphicsOverlays: [model.routeGraphicsOverlay])
.task {
model.makeGraphics()
do {
try await model.solveRoute(stops: model.demoStops)
} catch {
print(error)
}
}
}
}

Use API key access tokens

Tutorials

Samples

Services