Skip to content

Sb vanishing tree experiment #6493

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import com.mapbox.maps.Style.Companion.MAPBOX_STREETS
import com.mapbox.maps.plugin.LocationPuck2D
import com.mapbox.maps.plugin.animation.camera
import com.mapbox.maps.plugin.gestures.gestures
import com.mapbox.maps.plugin.locationcomponent.OnIndicatorPositionChangedListener
import com.mapbox.maps.plugin.locationcomponent.location
import com.mapbox.navigation.base.TimeFormat
import com.mapbox.navigation.base.extensions.applyDefaultNavigationOptions
Expand Down Expand Up @@ -141,6 +142,13 @@ class MapboxNavigationActivity : AppCompatActivity() {
private lateinit var routeArrowView: MapboxRouteArrowView
private val routeArrowAPI: MapboxRouteArrowApi = MapboxRouteArrowApi()

private val locationComponent by lazy {
binding.mapView.location.apply {
setLocationProvider(navigationLocationProvider)
enabled = true
}
}

/* ----- Voice instruction callbacks ----- */
private val voiceInstructionsObserver =
VoiceInstructionsObserver { voiceInstructions ->
Expand Down Expand Up @@ -388,6 +396,7 @@ class MapboxNavigationActivity : AppCompatActivity() {
// initialize route line
val mapboxRouteLineOptions = MapboxRouteLineOptions.Builder(this)
.withRouteLineBelowLayerId("road-label")
.withVanishingRouteLineEnabled(true)
.build()
routeLineAPI = MapboxRouteLineApi(mapboxRouteLineOptions)
routeLineView = MapboxRouteLineView(mapboxRouteLineOptions)
Expand Down Expand Up @@ -434,6 +443,7 @@ class MapboxNavigationActivity : AppCompatActivity() {

override fun onStart() {
super.onStart()
locationComponent.addOnIndicatorPositionChangedListener(onPositionChangedListener)
mapboxNavigation.registerRoutesObserver(routesObserver)
mapboxNavigation.registerNavigationSessionStateObserver(navigationSessionStateObserver)
mapboxNavigation.registerRouteProgressObserver(routeProgressObserver)
Expand All @@ -443,6 +453,7 @@ class MapboxNavigationActivity : AppCompatActivity() {

override fun onStop() {
super.onStop()
locationComponent.removeOnIndicatorPositionChangedListener(onPositionChangedListener)
mapboxNavigation.unregisterRoutesObserver(routesObserver)
mapboxNavigation.unregisterNavigationSessionStateObserver(navigationSessionStateObserver)
mapboxNavigation.unregisterRouteProgressObserver(routeProgressObserver)
Expand All @@ -469,7 +480,7 @@ class MapboxNavigationActivity : AppCompatActivity() {
RouteOptions.builder()
.applyDefaultNavigationOptions()
.applyLanguageAndVoiceUnitOptions(this)
.coordinatesList(listOf(origin, destination))
.coordinatesList(listOf(Point.fromLngLat(-122.37033971197376, 45.5794559098664), Point.fromLngLat(-122.37006184362927, 45.57944922334681))) // fixme
.layersList(listOf(mapboxNavigation.getZLevel(), null))
.build(),
object : NavigationRouterCallback {
Expand Down Expand Up @@ -527,4 +538,12 @@ class MapboxNavigationActivity : AppCompatActivity() {
private companion object {
private const val LOG_CATEGORY = "MapboxNavigationActivity"
}

private val onPositionChangedListener = OnIndicatorPositionChangedListener { point ->
val result = routeLineAPI.updateTraveledRouteLine(point)
mapboxMap.getStyle()?.apply {
// Render the result to update the map.
routeLineView.renderRouteLineUpdate(this, result)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ class MapboxRouteLineAndArrowActivity : AppCompatActivity(), OnMapLongClickListe
null,
ContextCompat.getDrawable(
this@MapboxRouteLineAndArrowActivity,
R.drawable.mapbox_navigation_puck_icon
R.drawable.custom_user_puck_icon
),
null,
null
Expand All @@ -227,7 +227,6 @@ class MapboxRouteLineAndArrowActivity : AppCompatActivity(), OnMapLongClickListe
// to the MapboxRouteLineApi to generate the data necessary to draw the route(s)
// on the map.
val routeLines = result.routes.map { RouteLine(it, null) }

routeLineApi.setRoutes(
routeLines
) { value ->
Expand All @@ -253,14 +252,6 @@ class MapboxRouteLineAndArrowActivity : AppCompatActivity(), OnMapLongClickListe
}

private val routeProgressObserver = RouteProgressObserver { routeProgress ->
// RouteLine: This line is only necessary if the vanishing route line feature
// is enabled.
routeLineApi.updateWithRouteProgress(routeProgress) { result ->
mapboxMap.getStyle()?.apply {
routeLineView.renderRouteLineUpdate(this, result)
}
}

// RouteArrow: The next maneuver arrows are driven by route progress events.
// Generate the next maneuver arrow update data and pass it to the view class
// to visualize the updates on the map.
Expand Down Expand Up @@ -373,6 +364,7 @@ class MapboxRouteLineAndArrowActivity : AppCompatActivity(), OnMapLongClickListe
}

override fun onMapLongClick(point: Point): Boolean {
Log.e("foobar", "point $point")
vibrate()
viewBinding.startNavigation.visibility = View.GONE
viewBinding.optionTrafficGradient.visibility = View.GONE
Expand All @@ -387,14 +379,15 @@ class MapboxRouteLineAndArrowActivity : AppCompatActivity(), OnMapLongClickListe
}
return false
}

//-122.36898496055935, 45.57909397202329
//-122.37006184362927, 45.57944922334681
fun findRoute(origin: Point?, destination: Point?) {
val routeOptions = RouteOptions.builder()
.applyDefaultNavigationOptions()
.applyLanguageAndVoiceUnitOptions(this)
.coordinatesList(listOf(origin, destination))
.coordinatesList(listOf(Point.fromLngLat(-122.37033971197376, 45.5794559098664), Point.fromLngLat(-122.37006184362927, 45.57944922334681))) //fixme
.layersList(listOf(mapboxNavigation.getZLevel(), null))
.alternatives(true)
.alternatives(false)
.build()
mapboxNavigation.requestRoutes(
routeOptions,
Expand Down Expand Up @@ -453,6 +446,7 @@ class MapboxRouteLineAndArrowActivity : AppCompatActivity(), OnMapLongClickListe
startSimulation(route)
}
}

viewBinding.mapView.gestures.addOnMapClickListener(mapClickListener)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,5 +64,4 @@
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:visibility="invisible"/>

</androidx.constraintlayout.widget.ConstraintLayout>
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ import com.mapbox.navigation.ui.utils.internal.ifNonNull
import com.mapbox.navigation.utils.internal.logE
import com.mapbox.navigation.utils.internal.logW
import com.mapbox.turf.TurfConstants
import com.mapbox.turf.TurfMeasurement
import com.mapbox.turf.TurfMisc
import kotlin.math.ln
import kotlin.math.max
Expand Down Expand Up @@ -716,7 +717,7 @@ internal object MapboxRouteLineUtils {
/**
* Decodes the route and produces [RouteLineGranularDistances].
*/
internal val granularDistancesProvider: (
val granularDistancesProvider: (
route: NavigationRoute,
) -> RouteLineGranularDistances? =
{ route: NavigationRoute ->
Expand Down Expand Up @@ -880,7 +881,6 @@ internal object MapboxRouteLineUtils {
stepsPoints: List<List<List<Point>>>
): RouteLineGranularDistances {
var distance = 0.0

val stepsArray = stepsPoints.map { pointsPerLeg ->
pointsPerLeg.map { stepPoints ->
// there can be a lot of points for each step
Expand Down Expand Up @@ -955,6 +955,69 @@ internal object MapboxRouteLineUtils {
)
}

/**
* Adds equally spaced [RouteLineDistancesIndex] points between each of the inputted steps and
* returns a collection of the original [RouteLineDistancesIndex] points with the newly created
* points between them.
*
* @param steps a collection of [RouteLineDistancesIndex] representing step points
* @return a collection of RouteLineDistancesIndex
*/
fun getFillerPointsForStepPoints(steps: Array<RouteLineDistancesIndex>): List<RouteLineDistancesIndex> {
val fillerPoints = mutableListOf<RouteLineDistancesIndex>()
steps.forEachIndexed { index, routeLineDistancesIndex ->
if (index < steps.lastIndex) {
getFillerPoints(routeLineDistancesIndex, steps[index + 1]).apply {
fillerPoints.addAll(this)
}
}
}
return fillerPoints
}

/**
* Creates equally spaced [RouteLineDistancesIndex] points between the start point and end points
* and returns a collection with the start point and end point with the newly created
* points between them.
*
* @param startPoint the starting RouteLineDistancesIndex
* @param endPoint the ending RouteLineDistancesIndex
* @return a collection of the start and end points and the points generated here
*/
private fun getFillerPoints(
startPoint: RouteLineDistancesIndex,
endPoint: RouteLineDistancesIndex
): List<RouteLineDistancesIndex> {
val gapDistanceInMeters = 1.0
val turfDistance = TurfMeasurement.distance(
startPoint.point,
endPoint.point,
TurfConstants.UNIT_METERS
)
val fillerPoints = mutableListOf<RouteLineDistancesIndex>()
val bearing = TurfMeasurement.bearing(startPoint.point, endPoint.point)
val numPointsToCreate = (turfDistance / gapDistanceInMeters).toInt()
val delta = startPoint.distanceRemaining - endPoint.distanceRemaining
val itemDistance = delta / numPointsToCreate
var lastCalculatedPoint = startPoint
var distanceRemaining = startPoint.distanceRemaining

fillerPoints.add(startPoint)
repeat(numPointsToCreate) {
val fillerPoint = TurfMeasurement.destination(
lastCalculatedPoint.point,
gapDistanceInMeters,
bearing,
TurfConstants.UNIT_METERS
)

distanceRemaining -= itemDistance
fillerPoints.add(RouteLineDistancesIndex(fillerPoint, distanceRemaining))
lastCalculatedPoint = fillerPoints.last()
}
return fillerPoints
}

private val generateRouteFeatureData: (
route: NavigationRoute,
identifier: String?
Expand Down
Loading