The Mapbox Directions API allows you to calculate routes between locations. You can get turn-by-turn directions, route geometry, distance, and duration for different routing profiles (driving, walking, cycling).
For complete API reference and details, see the official Mapbox Directions API documentation.
To use the Directions API, you need to:
- Reference the
MapBehaviourCorecomponent to access the map's file source. - Create a
MapboxDirectionsApiinstance with the file source. - Build a
DirectionResourcewith your origin and destination coordinates. - Query the API and handle the response in a callback.
Here's a simple example:
using UnityEngine;
using Mapbox.Unity.Map;
using Mapbox.DirectionsApi;
using Mapbox.DirectionsApi.Response;
using Mapbox.Utils;
public class DirectionsExample : MonoBehaviour
{
public MapBehaviourCore MapCore;
private MapboxDirectionsApi _directionsApi;
void Start()
{
MapCore.Initialized += map =>
{
_directionsApi = new MapboxDirectionsApi(map.MapService.FileSource);
GetRoute();
};
}
void GetRoute()
{
// Define two coordinates: San Francisco to Los Angeles
var origin = new LatitudeLongitude(37.7749, -122.4194);
var destination = new LatitudeLongitude(34.0522, -118.2437);
// Create a directions request with driving profile
var directionResource = new DirectionResource(
new[] { origin, destination },
RoutingProfile.Driving
);
// Query the API
_directionsApi.Query(directionResource, response =>
{
if (response == null || response.Routes == null || response.Routes.Count == 0)
{
Debug.Log("No route found");
return;
}
var route = response.Routes[0];
Debug.Log($"Route found!");
Debug.Log($"Distance: {route.Distance} meters");
Debug.Log($"Duration: {route.Duration} seconds");
Debug.Log($"Geometry points: {route.Geometry.Count}");
});
}
}- The script waits for the map to initialize and gets access to the file source.
- Creates a
MapboxDirectionsApiinstance for making routing queries. - Defines a route from San Francisco to Los Angeles using latitude/longitude coordinates.
- Requests a driving route between the two points.
- When the response arrives, it logs the route distance (in meters), duration (in seconds), and number of geometry points.
You can choose from three routing profiles:
RoutingProfile.Driving- Optimized for car travelRoutingProfile.Walking- Optimized for pedestriansRoutingProfile.Cycling- Optimized for bicycle travel
The DirectionResource supports several optional parameters:
var directionResource = new DirectionResource(coordinates, RoutingProfile.Driving)
{
Alternatives = true, // Get alternative routes
Steps = true, // Include turn-by-turn instructions
Overview = Overview.Full // Get full route geometry
};The DirectionsResponse contains:
Routes- List of possible routes (usually one, unless alternatives are requested)Waypoints- Snapped waypoint locationsCode- Response status code ("Ok" if successful)
Each Route includes:
Distance- Total route distance in metersDuration- Estimated travel time in secondsGeometry- List of lat/lng coordinates forming the route pathWeight- Route weight (typically duration-based)