mapbox / mapbox-navigation-android-examples

Other
53 stars 46 forks source link

introduce alternative routes example #103

Closed dzinad closed 2 years ago

dzinad commented 2 years ago

Refs #51.

VysotskiVadim commented 2 years ago

https://user-images.githubusercontent.com/6190346/175510897-7da0e595-1148-45dc-975c-9c3e2427a0de.mp4

Noticed an issue:

  1. Open example
  2. Click start navigation

Expected result: camera stays still, routes appear and navigation starts Actual result: camera moves to a new position, routes appear and navigationstarts

dzinad commented 2 years ago

Noticed an issue: Open example Click start navigation Expected result: camera stays still, routes appear and navigation starts Actual result: camera moves to a new position, routes appear and navigation starts

How did you do it? Works fine for me... https://user-images.githubusercontent.com/32109537/175542063-8c59c0c5-96c8-44b2-ab35-d36513567ca1.mp4

abhishek1508 commented 2 years ago

https://github.com/mapbox/mapbox-navigation-android-examples/pull/103#issuecomment-1165406837

This happens to me as well. As I had mentioned before, if you take a look at eg: ShowManeuverActivity and make the changes by copying the code from it as it is these issues will resolve. Let us also remove routeProgressObserver and onIndicatorPositionChangedListener as they are not relevant to this example and serve no purpose. I have attached the diff below

diff --git a/app/src/main/java/com/mapbox/navigation/examples/alternative/ShowAlternativeRoutesActivity.kt b/app/src/main/java/com/mapbox/navigation/examples/alternative/ShowAlternativeRoutesActivity.kt
index f14b9fd..d4850f0 100644
--- a/app/src/main/java/com/mapbox/navigation/examples/alternative/ShowAlternativeRoutesActivity.kt
+++ b/app/src/main/java/com/mapbox/navigation/examples/alternative/ShowAlternativeRoutesActivity.kt
@@ -175,12 +175,12 @@ class ShowAlternativeRoutesActivity : AppCompatActivity() {
      * Used to enable the vanishing route line feature. For more details see [RenderRouteLineActivity].
      */
     private val onIndicatorPositionChangedListener = OnIndicatorPositionChangedListener { point ->
-        routeLineApi.updateTraveledRouteLine(point).let {
+        /*routeLineApi.updateTraveledRouteLine(point).let {
             routeLineView.renderRouteLineUpdate(
                 binding.mapView.getMapboxMap().getStyle()!!,
                 it
             )
-        }
+        }*/
     }

     /**
@@ -208,11 +208,11 @@ class ShowAlternativeRoutesActivity : AppCompatActivity() {
      * Gets notified with progress along the currently active route.
      */
     private val routeProgressObserver = RouteProgressObserver { routeProgress ->
-        routeLineApi.updateWithRouteProgress(routeProgress) { result ->
+        /*routeLineApi.updateWithRouteProgress(routeProgress) { result ->
             binding.mapView.getMapboxMap().getStyle()?.apply {
                 routeLineView.renderRouteLineUpdate(this, result)
             }
-        }
+        }*/
     }

     /**
@@ -266,6 +266,11 @@ class ShowAlternativeRoutesActivity : AppCompatActivity() {
         binding = ActivityShowAlternativeRoutesBinding.inflate(layoutInflater)
         setContentView(binding.root)

+        binding.mapView.location.apply {
+            setLocationProvider(navigationLocationProvider)
+            enabled = true
+        }
+
         // initialize Mapbox Navigation
         mapboxNavigation = MapboxNavigationProvider.create(
             NavigationOptions.Builder(this)
@@ -278,9 +283,30 @@ class ShowAlternativeRoutesActivity : AppCompatActivity() {
                 )
                 .build()
         )
-        initNavigation()
-        initStyle()
-        initListeners()
+
+        binding.mapView.getMapboxMap().loadStyleUri(
+            NavigationStyles.NAVIGATION_DAY_STYLE
+        ) {
+            updateCamera(originPoint)
+            binding.actionButton.visibility = View.VISIBLE
+        }
+
+        binding.actionButton.text = "Set Route"
+        binding.actionButton.setOnClickListener {
+            when (mapboxNavigation.getNavigationRoutes().isEmpty()) {
+                true -> {
+                    binding.actionButton.text = "Start Navigation"
+                    findRoute(originPoint, destinationPoint)
+                }
+                false -> {
+                    binding.actionButton.visibility = View.GONE
+                    startSimulation(mapboxNavigation.getNavigationRoutes().first())
+                }
+            }
+        }
+        binding.mapView.gestures.addOnMapClickListener(mapClickListener)
+
+        mapboxNavigation.startTripSession()
     }

     override fun onStart() {
@@ -309,17 +335,6 @@ class ShowAlternativeRoutesActivity : AppCompatActivity() {
         mapboxNavigation.onDestroy()
     }

-    @SuppressLint("MissingPermission")
-    private fun initListeners() {
-        binding.startNavigation.setOnClickListener {
-            mapboxNavigation.startTripSession()
-            binding.startNavigation.visibility = View.GONE
-            findRoute(originPoint, destinationPoint)
-        }
-
-        binding.mapView.gestures.addOnMapClickListener(mapClickListener)
-    }
-
     /**
      * Request routes between the two points.
      */
@@ -340,7 +355,6 @@ class ShowAlternativeRoutesActivity : AppCompatActivity() {
                 ) {
                     if (routes.isNotEmpty()) {
                         mapboxNavigation.setNavigationRoutes(routes)
-                        startSimulation(routes.first())
                     }
                 }

@@ -359,20 +373,14 @@ class ShowAlternativeRoutesActivity : AppCompatActivity() {
     }

     private fun startSimulation(route: NavigationRoute) {
-        mapboxReplayer.stop()
-        mapboxReplayer.clearEvents()
-        val replayData = ReplayRouteMapper().mapDirectionsRouteGeometry(route.directionsRoute)
-        mapboxReplayer.pushEvents(replayData)
-        mapboxReplayer.seekTo(replayData[0])
-        mapboxReplayer.play()
-    }
-
-    @SuppressLint("MissingPermission")
-    private fun initStyle() {
-        binding.mapView.getMapboxMap().loadStyleUri(
-            NavigationStyles.NAVIGATION_DAY_STYLE
-        ) {
-            updateCamera(originPoint)
+        mapboxReplayer.run {
+            stop()
+            clearEvents()
+            pushRealLocation(this@ShowAlternativeRoutesActivity, 0.0)
+            val replayEvents = ReplayRouteMapper().mapDirectionsRouteGeometry(route.directionsRoute)
+            pushEvents(replayEvents)
+            seekTo(replayEvents.first())
+            play()
         }
     }

@@ -389,16 +397,4 @@ class ShowAlternativeRoutesActivity : AppCompatActivity() {
             mapAnimationOptions
         )
     }
-
-    @SuppressLint("MissingPermission")
-    private fun initNavigation() {
-        binding.mapView.location.apply {
-            setLocationProvider(navigationLocationProvider)
-            addOnIndicatorPositionChangedListener(onIndicatorPositionChangedListener)
-            enabled = true
-        }
-        mapboxReplayer.pushRealLocation(this, 0.0)
-        mapboxReplayer.playbackSpeed(1.5)
-        mapboxReplayer.play()
-    }
 }
diff --git a/app/src/main/java/com/mapbox/navigation/examples/alternative/res/layout/activity_show_alternative_routes.xml b/app/src/main/java/com/mapbox/navigation/examples/alternative/res/layout/activity_show_alternative_routes.xml
index 8b805a3..2a6dac3 100644
--- a/app/src/main/java/com/mapbox/navigation/examples/alternative/res/layout/activity_show_alternative_routes.xml
+++ b/app/src/main/java/com/mapbox/navigation/examples/alternative/res/layout/activity_show_alternative_routes.xml
@@ -13,14 +13,23 @@
         app:layout_constraintStart_toStartOf="parent"
         app:layout_constraintTop_toTopOf="parent" />

+
+
     <androidx.appcompat.widget.AppCompatButton
-        android:id="@+id/startNavigation"
-        android:layout_width="match_parent"
+        android:id="@+id/actionButton"
+        android:layout_width="0dp"
         android:layout_height="wrap_content"
+        android:padding="12dp"
+        android:textColor="@android:color/white"
+        android:layout_marginEnd="8dp"
+        android:layout_marginStart="8dp"
+        android:layout_marginBottom="8dp"
+        android:textAllCaps="false"
+        android:visibility="gone"
+        android:background="@drawable/mapbox_button"
         app:layout_constraintEnd_toEndOf="parent"
         app:layout_constraintStart_toStartOf="parent"
         app:layout_constraintBottom_toBottomOf="parent"
-        android:text="Start Navigation"
-        android:background="@color/colorPrimary"/>
+        />

 </androidx.constraintlayout.widget.ConstraintLayout>
\ No newline at end of file

/cc @dzinad

dzinad commented 2 years ago

This happens to me as well. As I had mentioned before, if you take a look at eg: ShowManeuverActivity and make the changes by copying the code from it as it is these issues will resolve. Let us also remove routeProgressObserver and onIndicatorPositionChangedListener as they are not relevant to this example and serve no purpose. I have attached the diff below

Thanks! I've applied most of those changes, however I left out the part with the second button click. I find it not really relevant to the example and that we can click just once. WDYT?