Fetch route between an origin and destination

This example shows:

  • How to fetch a route using RouteFetcher.getRoute with RouteRequestParams
    • .origin(origin)

    • .destination(destination)

    • .alternatives(true)

    • .altCount(2)

    • .avoid(avoid)

    • .language("en")

    • .waypoints(waypoints)

documentation image

For all code examples, refer to Navigation Code Examples

activity_fetch_routes.xml view source

1
<?xml version="1.0" encoding="utf-8"?>
2
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3
android:layout_width="match_parent"
4
android:layout_height="match_parent"
5
android:orientation="vertical"
6
android:padding="10dp">
7
8
<LinearLayout
9
android:layout_width="wrap_content"
10
android:layout_height="wrap_content">
11
<Button
12
android:id="@+id/fetchRoute"
13
android:layout_width="wrap_content"
14
android:layout_height="wrap_content"
15
android:text="@string/fetch_route_with_routefetcher"/>
16
</LinearLayout>
17
18
<TextView
19
android:id="@+id/routeGeometry"
20
android:layout_width="wrap_content"
21
android:layout_height="wrap_content"
22
android:layout_marginTop="10dp"/>
23
24
<ProgressBar
25
android:id="@+id/progress"
26
android:visibility="gone"
27
android:layout_width="wrap_content"
28
android:layout_height="wrap_content"
29
android:layout_gravity="center"/>
30
31
</LinearLayout>

FetchRoutesActivity view source

1
package ai.nextbillion.navigation.demo.activity;
2
3
import android.os.Bundle;
4
import android.view.View;
5
import android.widget.Button;
6
import android.widget.ProgressBar;
7
import android.widget.TextView;
8
9
import java.util.ArrayList;
10
import java.util.List;
11
12
import ai.nextbillion.kits.directions.models.DirectionsResponse;
13
import ai.nextbillion.kits.directions.models.DirectionsRoute;
14
import ai.nextbillion.kits.directions.models.RouteRequestParams;
15
import ai.nextbillion.kits.geojson.Point;
16
import ai.nextbillion.navigation.core.routefetcher.RequestParamConsts;
17
import ai.nextbillion.navigation.core.routefetcher.RouteFetcher;
18
import ai.nextbillion.navigation.demo.R;
19
import androidx.appcompat.app.AppCompatActivity;
20
import retrofit2.Call;
21
import retrofit2.Callback;
22
import retrofit2.Response;
23
24
public class FetchRoutesActivity extends AppCompatActivity implements View.OnClickListener {
25
private Button fetchRoute;
26
private TextView routeGeometry;
27
private DirectionsRoute directionsRoute;
28
private ProgressBar progress;
29
@Override
30
protected void onCreate(Bundle savedInstanceState) {
31
super.onCreate(savedInstanceState);
32
setContentView(R.layout.activity_fetch_routes);
33
fetchRoute = findViewById(R.id.fetchRoute);
34
routeGeometry = findViewById(R.id.routeGeometry);
35
progress = findViewById(R.id.progress);
36
fetchRoute.setOnClickListener(this);
37
}
38
39
@Override
40
public void onClick(View view) {
41
if (view.getId() == R.id.fetchRoute) {
42
progress.setVisibility(View.VISIBLE);
43
Point origin = Point.fromLngLat(103.75986708439264, 1.312533169133601);
44
Point destination = Point.fromLngLat(103.77982271935586, 1.310473772283314);
45
46
List<String> avoid = new ArrayList<>();
47
avoid.add(RequestParamConsts.AVOID_HIGHWAY);
48
49
List<Point> waypoints = new ArrayList<>();
50
waypoints.add(Point.fromLngLat(103.76974384826651,1.3207136058314086));
51
52
RouteRequestParams.Builder builder = RouteRequestParams.builder()
53
.origin(origin)
54
.destination(destination)
55
.alternatives(true)
56
.altCount(2)
57
.avoid(avoid)
58
.language("en")
59
.waypoints(waypoints)
60
.departureTime((int) (System.currentTimeMillis()/1000));
61
62
RouteFetcher.getRoute(builder.build(), new Callback<DirectionsResponse>() {
63
@Override
64
public void onResponse(Call<DirectionsResponse> call, Response<DirectionsResponse> response) {
65
progress.setVisibility(View.GONE);
66
//start navigation with the route we just fetched.
67
if (response.body() != null && !response.body().routes().isEmpty()) {
68
directionsRoute = response.body().routes().get(0);
69
routeGeometry.setText(String.format("Route Geometry: %s", directionsRoute.geometry()));
70
}
71
}
72
73
@Override
74
public void onFailure(Call<DirectionsResponse> call, Throwable t) {
75
progress.setVisibility(View.GONE);
76
}
77
});
78
}
79
}
80
81
}

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.

© 2024 NextBillion.ai all rights reserved.