Build routes
The entry point to the NaviKit SDK API is the Navigation class. It contains the main functions for navigation and route building.
Create Navigation
To create new navigation, call NavigationFactory.createNavigation.
final navigation = NavigationFactory.createNavigation(DrivingRouterType.Combined);
Request routes
With Navigation, you can build optimal routes using intermediate route points.
To request routes, use the Navigation.requestRoutes method.
// Coordinates routes are requested for
final requestPoints = [
RequestPoint(
const Point(latitude: 25.190614, longitude: 55.265616), RequestPointType.Waypoint, null, null
),
RequestPoint(
const Point(latitude: 25.187532, longitude: 55.275413), RequestPointType.Waypoint, null, null
),
RequestPoint(
const Point(latitude: 25.189279, longitude: 55.282246), RequestPointType.Viapoint, null, null
),
RequestPoint(
const Point(latitude: 25.196605, longitude: 55.280940), RequestPointType.Waypoint, null, null
),
];
navigation.requestRoutes(
points: requestPoints,
initialAzimuth: navigation.guidance.location?.heading,
routesCount: 3,
);
The first argument is the RequestPoint list of coordinates the route passes through.
Route points are divided into two types:
- Waypoint: Used for the start and destination points. These include the departure, the destination, and intermediate stops.
- Viapoint: Used for correcting the route. The route will pass through all the via points.
The second argument is the DrivingOptions object with the guidance parameters:
initialAzimuth
: Sets the user direction azimuth at the starting point of the route.routesCount
: The number of routes to build.
Note
The number of simultaneous route requests through Navigation is limited — there can only be one active request at a time. If you create a new request before the previous one is completed, the previous one is canceled.
To learn how to get the route request results, go to Route request results.
Route request by URI
In Navigation, you can request a route with the route URI saved earlier using the Navigation.resolveUri method.
To learn how to get the route request results, go to Route request results.
Request alternate routes
A list of alternate routes may be built during guidance along the selected route in NaviKit SDK, they're called global alternatives. They need a separate request.
Use the Navigation.requestAlternatives asynchronous call.
To learn how to get alternate request results, go to Route request results.
Events
Using the NavigationListener interface, you can subscribe to the Navigation entity to get events related to internal navigation status changes.
Route request results
The Navigation.requestRoutes and Navigation.requestAlternatives calls are asynchronous since they use a network request. To process the request results, subscribe to navigation events using the NavigationListener interface.
final class NavigationListenerImpl implements NavigationListener {
@override
void onRoutesBuilt() {
final routes = navigation.routes;
final fastestRoute = routes[0];
// Routes received successfully
}
@override
void onRoutesRequestError(Error error) {
// An error occured when requesting routes...
}
// Override other listener's methods
}
// You have to subscribe to Navigation before calling the route/alternative request
final NavigationListener listener = NavigationListenerImpl();
navigation.addListener(listener);
The NavigationListener.onRoutesBuilt handler method is called after a successful route request. You can get the route request results using the Navigation.routes method. It returns a list of routes that match the last request. The routes are sorted by travel time, with the quickest first.
To learn about route request errors, use the NavigationListener.onRoutesRequestError method.
Create a new request
Using NavigationListener, you can subscribe to new requests for Navigation by the type of routes requested.
final class NavigationListenerImpl implements NavigationListener {
@override
void onRoutesRequested(List<RequestPoint> points) {
// ...
}
@override
void onAlternativesRequested(DrivingRoute currentRoute) {
// ...
}
// Override other listener's methods
}
final NavigationListener listener = NavigationListenerImpl();
navigation.addListener(listener);
- NavigationListener.onRoutesRequested notifies you about new route requests.
- NavigationListener.onAlternativesRequested notifies you about new alternate route requests.
Reset routes
Using the NavigationListener.onResetRoutes, you can subscribe to resetting route status events.
Route request parameters
Route requests may take into account additional parameters that use Navigation methods:
- Navigation.avoidTolls: Avoid toll roads.
- Navigation.avoidUnpaved: Avoid unpaved roads.
- Navigation.avoidPoorConditions: Avoid roads in poor condition.
Navigation for trucks
NaviKit SDK can request routes built around the special characteristics of different vehicle types. To use vehicle parameters, you can change them using Navigation.vehicleOptions.
Vehicle parameters are passed using the DrivingVehicleOptions class. It stores all data related to truck restrictions. For example, vehicle size (height, width, and length), weight parameters (actual weight, allowed weight, axle load, and load capacity), and other information (such as trailer availability and eco class).
Here's an example where routes are requested for a truck that's 4.5 meters high and weighs 45 tons:
navigation.vehicleOptions = DrivingVehicleOptions(
vehicleType: DrivingVehicleType.Truck,
weight: 45.0,
height: 4.5,
);
navigation.requestRoutes(somePoints, someInitialAzimuth, someRoutesCount)
After the route is requested with DrivingVehicleOptions, it's built around the passed parameters for the selected vehicle.