Fetch route between an origin and destination
This example shows:
- How to fetch a route using
RouteFetcher.getRoute
withRouteRequestParams
-
.origin(origin)
-
.destination(destination)
-
.alternatives(true)
-
.altCount(2)
-
.avoid(avoid)
-
.language("en")
-
.waypoints(waypoints)
-
![documentation image](https://static.nextbillion.io/docs-next/docs/navigation/android/draw-route-line.jpg)
For all code examples, refer to Navigation Code Examples
activity_fetch_routes.xml view source
FetchRoutesActivity view source
Code Highlights
This code example shows how to fetch a route using RouteFetcher.getRoute()
The response of the API would be a DirectionsRoute object, and in this example, we have printed the result data on the device screen
Code summary
Fetch Routes
-
A
RouteRequestParams
object is constructed using the builder pattern and the provided parameters, including-
origin
: It represents the starting point of the route. It is of type Point and contains latitude and longitude coordinates. -
destination
: It represents the destination point of the route. It is also of type Point and contains latitude and longitude coordinates.
-
-
alternatives
: A boolean value indicating whether alternative routes should be provided. If set to true, multiple alternative routes may be returned in the response. -
altCount
: specifies the number of alternative routes to request. It is an integer value indicating the desired count of alternative routes. -
avoid
: A list of strings specifying the types of features or road elements to avoid on the route. For example, "highway" can be added to avoid highways -
language
: specifies the language for the route instructions and text in the response. It is a string representing the language code (e.g., "en" for English). -
waypoints
: a list of intermediate points (waypoints) to include in the route. Each waypoint is represented by a Point object with latitude and longitude coordinates.
These parameters allow you to customize the route request based on your specific needs, such as defining the origin and destination, requesting alternative routes, avoiding certain features or road elements, specifying the language for instructions, adding waypoints, and setting the departure time.
RouteFetcher.getRoute(builder.build(), new Callback<DirectionsResponse>() {...})
invokes the getRoute() method of RouteFetcher by passing the built RouteRequestParams
object and a callback for handling the response.