Map Display and Navigation
This section provides an overview of the core features and capabilities of the Navigation SDK for displaying maps and navigating routes. It covers topics such as starting navigation, using the helper class, defining coordinates and geometries, and handling route response callbacks.
Start a Navigation
The guide explains how to use the SDK to fetch a route and start navigation. It first explains how to fetch a route by providing a pair of coordinates (origin and destination), and then it shows how to use the fetched route(s) to start navigation by building a launch configuration and calling NavigationLauncher.startNavigation()
Helper Class
(If you are already aware of the Helper classes, please skip this section and jump to the next section: Fetch a route)
In our navigation SDK, we have put some boilerplate code to start navigations together in NBNavigation to make our life easier. Nextbillion.ai’s Navigation API supports two routing planning modes,
-
Plan route with coordinates: request a route with origin and destination (and middle waypoints if any).
-
Plan a route with geometry string: request a route with geometry data (an encoded polyline)
Below are the function signatures of this helper class, and it is naturally grouped by two modes - Coordinate Mode and Geometry Mode. We cover both of them in following sections:
Coordinate Mode
-
Origin + Destination + Simplified callback
-
Origin + Destination + Request Params Builder + Simplified callback
-
Origin + Destination + Request Params Builder + Retrofit callback
-
Origin + Destination + Retrofit callback
-
Origin + Destination + mode + Retrofit callback
-
Origin + Destination + mode + unit + Retrofit callback
-
Origin + Destination + Request Params + Retrofit callback
Geometry Mode
-
Geometry + GeometryType
-
Geometry + GeometryType + Request Params
-
Geometry + GeometryType + Retry (once retry is true and failed to retrieve route based on geometry, the SDK will populate the first and last coordinates from geometry to re-fetch a route)
-
Geometry + GeometryType + Request Params + Retry (once retry is true and failed to retrieve route based on geometry, the SDK will populate the first and last coordinates from geometry to re-fetch a route)
-
Geometry + GeometryType + Origin + Destination (once we failed to retrieve a route based on geometry, the SDK will re-fetch a route with the provided origin and destination coordinates)
Route response callback
As we can tell from the parameters of functions above, there are two different callbacks to receive the fetched routes:
-
NextbillionRoutingCallback is a custom callback interface for handling routing related events in the SDK. The purpose of this callback interface is to simplify routing-related response handling by providing predefined methods for success, failure, or other events.
-
Callback<DirectionsResponse> is a general-purpose callback class used for handling HTTP request responses, specifically tailored to the 'DirectionsResponse' data type in the context of routing.
Request Parameters
RouteRequestParams is a data structure that allows developers to modify the parameters of route planning, which includes parameters:
below are the default values:
Fetch a route
The minimum requirement to fuel a route fetching is a pair of coordinates, an origin point, and a destination point, in this docs, we are using two fixed coordinates(In Singapore):
And then utilize the functions from “NBNavigation”.
The simplest function/static method only requires three parameters, two coordinates, and an instance of the Retrofit’s Callback, with this function, we can populate the response and get the route instance, we are also able to handle HTTP status code and errors by ourselves.
The code snippet above is utilizing the NBNavigation.fetchRoute() method to retrieve a route between a given origin and destination coordinates. The method takes in 3 parameters, origin and destination coordinates and a callback object.
The callback object provides two observer methods, onResponse() and onFailure() for handling the API response. In case of a successful API call, the method onResponse() will be invoked, in which we can retrieve the first route from the response and save it in the variable route. Subsequently, it initiates navigation with this route.
On the other hand, if there is a failure in the API call, the method onFailure() will be executed, it can be used to handle the exception or display an error message to the developer/user.
Start navigation
After we fetch a route, the next step is to build a launching configuration that is composed of a route and set shouldSimulateRoute as true if we want to simulate the navigation.
Start navigation with the launcher configuration we just built.
This line of code will start the navigation using the NavigationView, the parameter activity here is the context of the current activity from which the navigation is starting, and the config object is provided by the builder method.
Put them together and we get:
It is assumed that the developer, who integrates this piece of code, already has the SDK set up and configured correctly, the import classes and interfaces are already added and the developer has the required permissions to start the navigation.