1
package ai.nextbillion.navigation.demo.activity;
3
import android.os.Bundle;
4
import android.view.View;
5
import android.widget.Button;
6
import android.widget.ProgressBar;
10
import ai.nextbillion.kits.directions.models.DirectionsResponse;
11
import ai.nextbillion.kits.directions.models.DirectionsRoute;
12
import ai.nextbillion.kits.directions.models.RouteRequestParams;
13
import ai.nextbillion.kits.geojson.Point;
14
import ai.nextbillion.maps.camera.CameraUpdate;
15
import ai.nextbillion.maps.camera.CameraUpdateFactory;
16
import ai.nextbillion.maps.core.MapView;
17
import ai.nextbillion.maps.core.NextbillionMap;
18
import ai.nextbillion.maps.core.OnMapReadyCallback;
19
import ai.nextbillion.maps.geometry.LatLng;
20
import ai.nextbillion.maps.location.modes.RenderMode;
21
import ai.nextbillion.navigation.core.routefetcher.RouteFetcher;
22
import ai.nextbillion.navigation.demo.R;
23
import ai.nextbillion.navigation.demo.utils.CameraAnimateUtils;
24
import ai.nextbillion.navigation.ui.NavLauncherConfig;
25
import ai.nextbillion.navigation.ui.NavigationLauncher;
26
import ai.nextbillion.navigation.ui.camera.CameraUpdateMode;
27
import ai.nextbillion.navigation.ui.camera.NavigationCameraUpdate;
28
import ai.nextbillion.navigation.ui.map.NavNextbillionMap;
29
import androidx.annotation.NonNull;
30
import androidx.appcompat.app.AppCompatActivity;
32
import retrofit2.Callback;
33
import retrofit2.Response;
35
public class DrawRouteLineActivity extends AppCompatActivity implements OnMapReadyCallback, View.OnClickListener {
37
private static final double DEFAULT_CAMERA_ZOOM = 14;
39
private Point origin = Point.fromLngLat(103.75986708439264, 1.312533169133601);
40
private Point destination = Point.fromLngLat(103.77982271935586, 1.310473772283314);
42
private MapView mapView;
43
private NavNextbillionMap navMap;
45
private Button fetchRoute;
46
private Button fetchRouteWithoutDuration;
47
private Button startNav;
48
private DirectionsRoute directionsRoute;
49
private List<DirectionsRoute> directionsRoutes;
50
private ProgressBar progress;
51
boolean showRouteDurationSymbol = true;
53
protected void onCreate(Bundle savedInstanceState) {
54
super.onCreate(savedInstanceState);
55
setContentView(R.layout.activity_draw_route_line);
56
mapView = findViewById(R.id.mapView);
57
mapView.onCreate(savedInstanceState);
58
mapView.getMapAsync(this);
59
fetchRoute = findViewById(R.id.fetchRoute);
60
fetchRouteWithoutDuration = findViewById(R.id.hideRouteDuration);
61
startNav = findViewById(R.id.startNav);
62
progress = findViewById(R.id.progress);
63
fetchRoute.setOnClickListener(this);
64
startNav.setOnClickListener(this);
65
fetchRouteWithoutDuration.setOnClickListener(this);
66
fetchRouteWithoutDuration.setEnabled(false);
67
startNav.setEnabled(false);
68
fetchRoute.setEnabled(false);
72
public void onMapReady(@NonNull NextbillionMap nextbillionMap) {
73
nextbillionMap.getStyle(style -> {
74
navMap = new NavNextbillionMap(mapView, nextbillionMap);
75
CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(new LatLng(origin.latitude(),origin.longitude()), DEFAULT_CAMERA_ZOOM);
76
NavigationCameraUpdate navigationCameraUpdate = new NavigationCameraUpdate(cameraUpdate);
77
navigationCameraUpdate.setMode(CameraUpdateMode.OVERRIDE);
78
navMap.retrieveCamera().update(navigationCameraUpdate, 1000);
79
fetchRoute.setEnabled(true);
80
fetchRouteWithoutDuration.setEnabled(true);
86
public void onClick(View view) {
87
if (view.getId() == R.id.fetchRoute) {
88
showRouteDurationSymbol = true;
90
} else if (view.getId() == R.id.hideRouteDuration) {
91
showRouteDurationSymbol = false;
93
} else if (view.getId() == R.id.startNav) {
94
NavLauncherConfig.Builder configBuilder = NavLauncherConfig.builder(directionsRoute);
95
configBuilder.locationLayerRenderMode(RenderMode.GPS);
96
configBuilder.shouldSimulateRoute(true);
97
NavigationLauncher.startNavigation(DrawRouteLineActivity.this, configBuilder.build());
101
private void fetchRoute() {
102
progress.setVisibility(View.VISIBLE);
104
RouteRequestParams.Builder builder = RouteRequestParams.builder()
106
.destination(destination)
108
.departureTime((int) (System.currentTimeMillis()/1000));
110
RouteFetcher.getRoute(builder.build(), new Callback<DirectionsResponse>() {
112
public void onResponse(Call<DirectionsResponse> call, Response<DirectionsResponse> response) {
113
progress.setVisibility(View.GONE);
114
//start navigation with the route we just fetched.
115
if (response.body() != null && !response.body().routes().isEmpty()) {
116
directionsRoute = response.body().routes().get(0);
117
directionsRoutes = response.body().routes();
119
startNav.setEnabled(true);
124
public void onFailure(Call<DirectionsResponse> call, Throwable t) {
125
progress.setVisibility(View.GONE);
130
private void drawRouteLine() {
131
navMap.removeRoute();
132
navMap.clearMarkers();
133
navMap.showRouteDurationSymbol(showRouteDurationSymbol);
134
navMap.drawRoute(directionsRoute);
135
frameCameraToRoute();
138
private void frameCameraToRoute() {
139
int[] padding = CameraAnimateUtils.createPadding(this);
140
CameraAnimateUtils.frameCameraToBounds(navMap, directionsRoutes, padding);
144
protected void onStart() {
150
protected void onResume() {
156
protected void onPause() {
162
public void onLowMemory() {
164
mapView.onLowMemory();
168
protected void onStop() {
174
protected void onDestroy() {
180
protected void onSaveInstanceState(@NonNull Bundle outState) {
181
super.onSaveInstanceState(outState);
182
mapView.onSaveInstanceState(outState);