Routing in CARTO Mobile SDK provides fastest path between two or more geographic locations.
The resulting Route includes waypoints and instructions. It can be displayed as turn-by-turn directions on your map, or just a line or list of instructions. Routing functionality through the Mobile SDK includes online routing, based on CARTOs online service (or third party services), and offline routing, which requires that you install an offline data package. Offline packages have to be downloaded via Package Manager.
Mobile SDK supports the following routing features:
Find the fastest route from A to B
Find the fastest route between X points, in a given order
Get the complete result by route geometry and display it on the map
Get instructions for navigation actions (turn left/right, u-turn, leave roundabout etc.)
Specify instruction details, such as the street name, turn angle, azimuth, distance and time for the next leg
Plan for turn restrictions and one-way streets as part of the route
Fast calculations even in offline mode, approximately 50-300 ms is expected, even for long routes
Multi-country route calculations, even in offline mode
Online routing requires that you create a simple call and request to calculate the route.
First create the CartoOnlineRoutingService and then the route with the calculateRoute request.
Note: Calculating online routing makes a network call, so do NOT do it in UI thread.
Offline routing requires a more complicated preparation of your offline map packages, listener events, package initialization, and routing calculation parameters.
First, you need to initialize a package manager and a listener to download packages, and then download routing map package using this. View the PackageManager page to find more about it.
If the required routing packages are downloaded and routing service is ready, you can calculate routing.
Define RoutingRequest with at least two points.
Calculate the route with the calculateRoute request, read response as RoutingResult.
Limitations of offline routing
CARTO Mobile SDK provides two built-in offline routing engines: the legacy routing engine is based on OSRM project and newer one is using Valhalla routing engine. The OSRM-based routing engine is better optimized for low memory usage and calculation speed, including very large road networks using Contraction Hierarchy representation and algorithms. However, this has some expected limitations:
Route profile is precalculated and hardcoded in the data. For different profiles, such as driving or walking, download different map data packages to accomodate for offline routing
Only the fastest route is calculated, there is no shortest route choice
There are no alternative routes provided
There is no live data in routing, traffic and temporarily closed roads do not appear
You have to download whole country package, no custom bounding box area downloads are possible
Valhalla routing engine is more flexible, but requires more memory and is slower.
Note: SDK itself does not include live navigation features, such as following a GPS location, initiating automatic recalculations, or guided voice instructions. These features can still be built on top of routing by your device application, and our sample apps have code for this.
To download Valhalla routing engine offline packages using PackageManager, use "routing:carto.streets" as source ID. You can then request list of country packages (we have all countries in the world covered, but some are divided to smaller parts), or use custom bounding box area to download route data for e.g. smaller metropolitan area.
Note that the routing is very similar to online routing, just the service name is different. Following code assumes that you already have PackageManager with correct packages, and have downloaded a package.