Skip to content

Commit 724d720

Browse files
committed
prototyping
1 parent 631c864 commit 724d720

File tree

3 files changed

+56
-8
lines changed

3 files changed

+56
-8
lines changed

examples/src/main/java/com/mapbox/navigation/examples/core/MapboxNavigationActivity.kt

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,8 @@ class MapboxNavigationActivity : AppCompatActivity() {
8989
// location puck integration
9090
private val navigationLocationProvider = NavigationLocationProvider()
9191

92+
private val waypoints = mutableListOf<Point>()
93+
9294
// camera
9395
private lateinit var navigationCamera: NavigationCamera
9496
private lateinit var viewportDataSource: MapboxNavigationViewportDataSource
@@ -398,7 +400,7 @@ class MapboxNavigationActivity : AppCompatActivity() {
398400
routeLineView.initializeLayers(style)
399401
// add long click listener that search for a route to the clicked destination
400402
binding.mapView.gestures.addOnMapLongClickListener { point ->
401-
findRoute(point)
403+
addWaypoint(point)
402404
true
403405
}
404406
}
@@ -459,24 +461,25 @@ class MapboxNavigationActivity : AppCompatActivity() {
459461
voiceInstructionsPlayer.shutdown()
460462
}
461463

462-
private fun findRoute(destination: Point) {
464+
private fun addWaypoint(destination: Point) {
463465
val origin = navigationLocationProvider.lastLocation?.let {
464466
Point.fromLngLat(it.longitude, it.latitude)
465467
} ?: return
466468

469+
waypoints.add(destination)
470+
467471
mapboxNavigation.requestRoutes(
468472
RouteOptions.builder()
469473
.applyDefaultNavigationOptions()
470474
.applyLanguageAndVoiceUnitOptions(this)
471-
.coordinatesList(listOf(origin, destination))
472-
.layersList(listOf(mapboxNavigation.getZLevel(), null))
475+
.coordinatesList(listOf(origin) + waypoints)
473476
.build(),
474477
object : NavigationRouterCallback {
475478
override fun onRoutesReady(
476479
routes: List<NavigationRoute>,
477480
routerOrigin: RouterOrigin
478481
) {
479-
setRouteAndStartNavigation(routes)
482+
setRoutePreview(routes)
480483
}
481484

482485
override fun onFailure(
@@ -493,9 +496,25 @@ class MapboxNavigationActivity : AppCompatActivity() {
493496
)
494497
}
495498

496-
private fun setRouteAndStartNavigation(route: List<NavigationRoute>) {
499+
500+
501+
private fun setRoutePreview(route: List<NavigationRoute>) {
502+
// set route
503+
mapboxNavigation.previewNavigationRoutes(route)
504+
binding.navigate.visibility = VISIBLE
505+
binding.navigate.setOnClickListener {
506+
setRoute()
507+
binding.navigate.visibility = INVISIBLE
508+
}
509+
510+
// move the camera to overview when new route is available
511+
navigationCamera.requestNavigationCameraToOverview()
512+
}
513+
514+
private fun setRoute() {
497515
// set route
498-
mapboxNavigation.setNavigationRoutes(route)
516+
mapboxNavigation.setPreviewedRoute()
517+
waypoints.clear()
499518

500519
// show UI elements
501520
binding.soundButton.visibility = VISIBLE
@@ -505,7 +524,7 @@ class MapboxNavigationActivity : AppCompatActivity() {
505524
binding.soundButton.unmuteAndExtend(2000L)
506525

507526
// move the camera to overview when new route is available
508-
navigationCamera.requestNavigationCameraToOverview()
527+
navigationCamera.requestNavigationCameraToFollowing()
509528
}
510529

511530
private fun clearRouteAndStopNavigation() {

examples/src/main/res/layout/layout_activity_navigation.xml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,16 @@
1313
app:layout_constraintStart_toStartOf="parent"
1414
app:layout_constraintTop_toTopOf="parent" />
1515

16+
<Button
17+
android:layout_width="match_parent"
18+
android:layout_height="wrap_content"
19+
android:id="@+id/navigate"
20+
android:visibility="invisible"
21+
android:text="Navigate"
22+
app:layout_constraintBottom_toBottomOf="parent"
23+
app:layout_constraintStart_toStartOf="parent"
24+
app:layout_constraintEnd_toEndOf="parent"/>
25+
1626
<androidx.cardview.widget.CardView
1727
android:id="@+id/tripProgressCard"
1828
android:layout_width="0dp"

libnavigation-core/src/main/java/com/mapbox/navigation/core/MapboxNavigation.kt

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -828,6 +828,25 @@ class MapboxNavigation @VisibleForTesting internal constructor(
828828
)
829829
}
830830

831+
fun previewNavigationRoutes(
832+
routes: List<NavigationRoute>,
833+
initialLegIndex: Int = 0,
834+
) {
835+
//TODO: parse alternatives metadata using NN and set it route alternatives controller
836+
directionsSession.setRoutes(routes, initialLegIndex, RoutesExtra.ROUTES_UPDATE_REASON_NEW)
837+
}
838+
839+
fun setPreviewedRoute() {
840+
val routes = directionsSession.routes
841+
if (routes.isNotEmpty()) {
842+
internalSetNavigationRoutes(
843+
routes,
844+
directionsSession.initialLegIndex,
845+
RoutesExtra.ROUTES_UPDATE_REASON_NEW
846+
)
847+
}
848+
}
849+
831850
/**
832851
* Requests road graph data update and invokes the callback on result.
833852
* Use this method if the frequency of application relaunch is too low

0 commit comments

Comments
 (0)