Skip to content

Commit fa78685

Browse files
Seth BourgetSeth Bourget
authored andcommitted
wip
1 parent 43cdcf5 commit fa78685

File tree

17 files changed

+1029
-549
lines changed

17 files changed

+1029
-549
lines changed

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

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package com.mapbox.navigation.examples.core
33
import android.annotation.SuppressLint
44
import android.content.res.Configuration
55
import android.content.res.Resources
6+
import android.graphics.Color
67
import android.location.Location
78
import android.os.Bundle
89
import android.view.View.INVISIBLE
@@ -12,14 +13,22 @@ import androidx.appcompat.app.AppCompatActivity
1213
import androidx.core.content.ContextCompat
1314
import com.mapbox.api.directions.v5.models.RouteOptions
1415
import com.mapbox.bindgen.Expected
16+
import com.mapbox.geojson.Feature
17+
import com.mapbox.geojson.FeatureCollection
1518
import com.mapbox.geojson.Point
1619
import com.mapbox.maps.CameraOptions
1720
import com.mapbox.maps.EdgeInsets
1821
import com.mapbox.maps.MapboxMap
22+
import com.mapbox.maps.Style
1923
import com.mapbox.maps.Style.Companion.MAPBOX_STREETS
24+
import com.mapbox.maps.extension.style.layers.generated.CircleLayer
25+
import com.mapbox.maps.extension.style.sources.generated.GeoJsonSource
26+
import com.mapbox.maps.extension.style.sources.generated.geoJsonSource
27+
import com.mapbox.maps.extension.style.sources.getSource
2028
import com.mapbox.maps.plugin.LocationPuck2D
2129
import com.mapbox.maps.plugin.animation.camera
2230
import com.mapbox.maps.plugin.gestures.gestures
31+
import com.mapbox.maps.plugin.locationcomponent.OnIndicatorPositionChangedListener
2332
import com.mapbox.maps.plugin.locationcomponent.location
2433
import com.mapbox.navigation.base.TimeFormat
2534
import com.mapbox.navigation.base.extensions.applyDefaultNavigationOptions
@@ -226,6 +235,8 @@ class MapboxNavigationActivity : AppCompatActivity() {
226235

227236
// update bottom trip progress summary
228237
binding.tripProgressView.render(tripProgressApi.getTripProgress(routeProgress))
238+
239+
//addPointToPixelMapPoints(routeLineAPI.getFillerPointsInTree())
229240
}
230241

231242
private val routesObserver = RoutesObserver { result ->
@@ -268,6 +279,21 @@ class MapboxNavigationActivity : AppCompatActivity() {
268279
logD("sessionId=${mapboxNavigation.getNavigationSessionState().sessionId}", LOG_CATEGORY)
269280
}
270281

282+
private val locationComponent by lazy {
283+
binding.mapView.location.apply {
284+
setLocationProvider(navigationLocationProvider)
285+
enabled = true
286+
}
287+
}
288+
289+
private val onPositionChangedListener = OnIndicatorPositionChangedListener { point ->
290+
val result = routeLineAPI.updateTraveledRouteLine(point)
291+
mapboxMap.getStyle()?.apply {
292+
// Render the result to update the map.
293+
routeLineView.renderRouteLineUpdate(this, result)
294+
}
295+
}
296+
271297
@SuppressLint("MissingPermission")
272298
override fun onCreate(savedInstanceState: Bundle?) {
273299
super.onCreate(savedInstanceState)
@@ -280,7 +306,7 @@ class MapboxNavigationActivity : AppCompatActivity() {
280306
this.locationPuck = LocationPuck2D(
281307
bearingImage = ContextCompat.getDrawable(
282308
this@MapboxNavigationActivity,
283-
R.drawable.mapbox_navigation_puck_icon
309+
R.drawable.custom_user_puck_icon
284310
)
285311
)
286312
setLocationProvider(navigationLocationProvider)
@@ -317,7 +343,7 @@ class MapboxNavigationActivity : AppCompatActivity() {
317343
// not handled
318344
}
319345
})
320-
346+
mapboxNavigation.setRerouteController(null)
321347
// initialize Navigation Camera
322348
viewportDataSource = MapboxNavigationViewportDataSource(
323349
binding.mapView.getMapboxMap()
@@ -388,6 +414,7 @@ class MapboxNavigationActivity : AppCompatActivity() {
388414
// initialize route line
389415
val mapboxRouteLineOptions = MapboxRouteLineOptions.Builder(this)
390416
.withRouteLineBelowLayerId("road-label")
417+
.withVanishingRouteLineEnabled(true)
391418
.build()
392419
routeLineAPI = MapboxRouteLineApi(mapboxRouteLineOptions)
393420
routeLineView = MapboxRouteLineView(mapboxRouteLineOptions)
@@ -402,6 +429,10 @@ class MapboxNavigationActivity : AppCompatActivity() {
402429
findRoute(point)
403430
true
404431
}
432+
433+
//fixme remove this
434+
initPointLayer(style)
435+
locationComponent.addOnIndicatorPositionChangedListener(onPositionChangedListener)
405436
}
406437

407438
// initialize view interactions
@@ -527,4 +558,29 @@ class MapboxNavigationActivity : AppCompatActivity() {
527558
private companion object {
528559
private const val LOG_CATEGORY = "MapboxNavigationActivity"
529560
}
561+
562+
private val LINE_END_LAYER_ID = "DRAW_UTIL_LINE_END_LAYER_ID"
563+
private val LINE_END_SOURCE_ID = "DRAW_UTIL_LINE_END_SOURCE_ID"
564+
private fun initPointLayer(style: Style) {
565+
if (!style.styleSourceExists(LINE_END_SOURCE_ID)) {
566+
geoJsonSource(LINE_END_SOURCE_ID) {}.bindTo(style)
567+
}
568+
569+
if (!style.styleLayerExists(LINE_END_LAYER_ID)) {
570+
CircleLayer(LINE_END_LAYER_ID, LINE_END_SOURCE_ID)
571+
.circleRadius(2.0)
572+
.circleOpacity(1.0)
573+
.circleColor(Color.BLACK)
574+
.bindTo(style)
575+
}
576+
}
577+
578+
// todo remove this
579+
private fun addPointToPixelMapPoints(points: List<Point>) {
580+
val features = points.map { Feature.fromGeometry(it) }
581+
582+
(mapboxMap.getStyle()!!.getSource(LINE_END_SOURCE_ID) as GeoJsonSource).apply {
583+
this.featureCollection(FeatureCollection.fromFeatures(features))
584+
}
585+
}
530586
}

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

Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,19 @@ import com.mapbox.android.core.location.LocationEngineResult
1616
import com.mapbox.android.gestures.Utils
1717
import com.mapbox.api.directions.v5.models.DirectionsRoute
1818
import com.mapbox.api.directions.v5.models.RouteOptions
19+
import com.mapbox.geojson.Feature
20+
import com.mapbox.geojson.FeatureCollection
1921
import com.mapbox.geojson.Point
2022
import com.mapbox.maps.CameraOptions
2123
import com.mapbox.maps.EdgeInsets
2224
import com.mapbox.maps.MapboxMap
2325
import com.mapbox.maps.Style
2426
import com.mapbox.maps.extension.observable.eventdata.MapLoadingErrorEventData
27+
import com.mapbox.maps.extension.style.layers.generated.CircleLayer
2528
import com.mapbox.maps.extension.style.layers.properties.generated.Visibility
29+
import com.mapbox.maps.extension.style.sources.generated.GeoJsonSource
30+
import com.mapbox.maps.extension.style.sources.generated.geoJsonSource
31+
import com.mapbox.maps.extension.style.sources.getSource
2632
import com.mapbox.maps.plugin.LocationPuck2D
2733
import com.mapbox.maps.plugin.animation.MapAnimationOptions
2834
import com.mapbox.maps.plugin.animation.camera
@@ -38,6 +44,7 @@ import com.mapbox.navigation.base.options.NavigationOptions
3844
import com.mapbox.navigation.base.route.RouterCallback
3945
import com.mapbox.navigation.base.route.RouterFailure
4046
import com.mapbox.navigation.base.route.RouterOrigin
47+
import com.mapbox.navigation.base.utils.DecodeUtils.completeGeometryToPoints
4148
import com.mapbox.navigation.core.MapboxNavigationProvider
4249
import com.mapbox.navigation.core.directions.session.RoutesObserver
4350
import com.mapbox.navigation.core.replay.MapboxReplayer
@@ -203,7 +210,7 @@ class MapboxRouteLineAndArrowActivity : AppCompatActivity(), OnMapLongClickListe
203210
null,
204211
ContextCompat.getDrawable(
205212
this@MapboxRouteLineAndArrowActivity,
206-
R.drawable.mapbox_navigation_puck_icon
213+
R.drawable.custom_user_puck_icon
207214
),
208215
null,
209216
null
@@ -227,7 +234,6 @@ class MapboxRouteLineAndArrowActivity : AppCompatActivity(), OnMapLongClickListe
227234
// to the MapboxRouteLineApi to generate the data necessary to draw the route(s)
228235
// on the map.
229236
val routeLines = result.routes.map { RouteLine(it, null) }
230-
231237
routeLineApi.setRoutes(
232238
routeLines
233239
) { value ->
@@ -253,14 +259,6 @@ class MapboxRouteLineAndArrowActivity : AppCompatActivity(), OnMapLongClickListe
253259
}
254260

255261
private val routeProgressObserver = RouteProgressObserver { routeProgress ->
256-
// RouteLine: This line is only necessary if the vanishing route line feature
257-
// is enabled.
258-
routeLineApi.updateWithRouteProgress(routeProgress) { result ->
259-
mapboxMap.getStyle()?.apply {
260-
routeLineView.renderRouteLineUpdate(this, result)
261-
}
262-
}
263-
264262
// RouteArrow: The next maneuver arrows are driven by route progress events.
265263
// Generate the next maneuver arrow update data and pass it to the view class
266264
// to visualize the updates on the map.
@@ -269,6 +267,8 @@ class MapboxRouteLineAndArrowActivity : AppCompatActivity(), OnMapLongClickListe
269267
// Render the result to update the map.
270268
routeArrowView.renderManeuverUpdate(this, arrowUpdate)
271269
}
270+
271+
//addPointToPixelMapPoints(routeLineApi.getFillerPointsInTree())
272272
}
273273

274274
// RouteLine: Below is a demonstration of selecting different routes. On a map click, a call
@@ -359,6 +359,7 @@ class MapboxRouteLineAndArrowActivity : AppCompatActivity(), OnMapLongClickListe
359359
locationEngineCallback
360360
)
361361
viewBinding.mapView.gestures.addOnMapLongClickListener(this)
362+
initPointLayer(style)
362363
},
363364
object : OnMapLoadErrorListener {
364365
override fun onMapLoadError(eventData: MapLoadingErrorEventData) {
@@ -394,7 +395,7 @@ class MapboxRouteLineAndArrowActivity : AppCompatActivity(), OnMapLongClickListe
394395
.applyLanguageAndVoiceUnitOptions(this)
395396
.coordinatesList(listOf(origin, destination))
396397
.layersList(listOf(mapboxNavigation.getZLevel(), null))
397-
.alternatives(true)
398+
.alternatives(false)
398399
.build()
399400
mapboxNavigation.requestRoutes(
400401
routeOptions,
@@ -453,6 +454,7 @@ class MapboxRouteLineAndArrowActivity : AppCompatActivity(), OnMapLongClickListe
453454
startSimulation(route)
454455
}
455456
}
457+
456458
viewBinding.mapView.gestures.addOnMapClickListener(mapClickListener)
457459
}
458460

@@ -514,4 +516,30 @@ class MapboxRouteLineAndArrowActivity : AppCompatActivity(), OnMapLongClickListe
514516
override fun onFailure(exception: Exception) {
515517
}
516518
}
519+
520+
// todo remove this
521+
private val LINE_END_LAYER_ID = "DRAW_UTIL_LINE_END_LAYER_ID"
522+
private val LINE_END_SOURCE_ID = "DRAW_UTIL_LINE_END_SOURCE_ID"
523+
private fun initPointLayer(style: Style) {
524+
if (!style.styleSourceExists(LINE_END_SOURCE_ID)) {
525+
geoJsonSource(LINE_END_SOURCE_ID) {}.bindTo(style)
526+
}
527+
528+
if (!style.styleLayerExists(LINE_END_LAYER_ID)) {
529+
CircleLayer(LINE_END_LAYER_ID, LINE_END_SOURCE_ID)
530+
.circleRadius(2.0)
531+
.circleOpacity(1.0)
532+
.circleColor(Color.BLACK)
533+
.bindTo(style)
534+
}
535+
}
536+
537+
// todo remove this
538+
private fun addPointToPixelMapPoints(points: List<Point>) {
539+
val features = points.map { Feature.fromGeometry(it) }
540+
541+
(mapboxMap.getStyle()!!.getSource(LINE_END_SOURCE_ID) as GeoJsonSource).apply {
542+
this.featureCollection(FeatureCollection.fromFeatures(features))
543+
}
544+
}
517545
}

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,5 +64,4 @@
6464
app:layout_constraintStart_toStartOf="parent"
6565
app:layout_constraintBottom_toBottomOf="parent"
6666
android:visibility="invisible"/>
67-
6867
</androidx.constraintlayout.widget.ConstraintLayout>

libnavui-maps/src/main/java/com/mapbox/navigation/ui/maps/internal/route/line/MapboxRouteLineUtils.kt

Lines changed: 65 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ import com.mapbox.navigation.ui.utils.internal.ifNonNull
5757
import com.mapbox.navigation.utils.internal.logE
5858
import com.mapbox.navigation.utils.internal.logW
5959
import com.mapbox.turf.TurfConstants
60+
import com.mapbox.turf.TurfMeasurement
6061
import com.mapbox.turf.TurfMisc
6162
import kotlin.math.ln
6263
import kotlin.math.max
@@ -716,7 +717,7 @@ internal object MapboxRouteLineUtils {
716717
/**
717718
* Decodes the route and produces [RouteLineGranularDistances].
718719
*/
719-
internal val granularDistancesProvider: (
720+
val granularDistancesProvider: (
720721
route: NavigationRoute,
721722
) -> RouteLineGranularDistances? =
722723
{ route: NavigationRoute ->
@@ -880,7 +881,6 @@ internal object MapboxRouteLineUtils {
880881
stepsPoints: List<List<List<Point>>>
881882
): RouteLineGranularDistances {
882883
var distance = 0.0
883-
884884
val stepsArray = stepsPoints.map { pointsPerLeg ->
885885
pointsPerLeg.map { stepPoints ->
886886
// there can be a lot of points for each step
@@ -955,6 +955,69 @@ internal object MapboxRouteLineUtils {
955955
)
956956
}
957957

958+
/**
959+
* Adds equally spaced [RouteLineDistancesIndex] points between each of the inputted steps and
960+
* returns a collection of the original [RouteLineDistancesIndex] points with the newly created
961+
* points between them.
962+
*
963+
* @param steps a collection of [RouteLineDistancesIndex] representing step points
964+
* @return a collection of RouteLineDistancesIndex
965+
*/
966+
fun getFillerPointsForStepPoints(steps: Array<RouteLineDistancesIndex>): List<RouteLineDistancesIndex> {
967+
val fillerPoints = mutableListOf<RouteLineDistancesIndex>()
968+
steps.forEachIndexed { index, routeLineDistancesIndex ->
969+
if (index < steps.lastIndex) {
970+
getFillerPoints(routeLineDistancesIndex, steps[index + 1]).apply {
971+
fillerPoints.addAll(this)
972+
}
973+
}
974+
}
975+
return fillerPoints
976+
}
977+
978+
/**
979+
* Creates equally spaced [RouteLineDistancesIndex] points between the start point and end points
980+
* and returns a collection with the start point and end point with the newly created
981+
* points between them.
982+
*
983+
* @param startPoint the starting RouteLineDistancesIndex
984+
* @param endPoint the ending RouteLineDistancesIndex
985+
* @return a collection of the start and end points and the points generated here
986+
*/
987+
private fun getFillerPoints(
988+
startPoint: RouteLineDistancesIndex,
989+
endPoint: RouteLineDistancesIndex
990+
): List<RouteLineDistancesIndex> {
991+
val gapDistanceInMeters = 1.0
992+
val turfDistance = TurfMeasurement.distance(
993+
startPoint.point,
994+
endPoint.point,
995+
TurfConstants.UNIT_METERS
996+
)
997+
val fillerPoints = mutableListOf<RouteLineDistancesIndex>()
998+
val bearing = TurfMeasurement.bearing(startPoint.point, endPoint.point)
999+
val numPointsToCreate = (turfDistance / gapDistanceInMeters).toInt()
1000+
val delta = startPoint.distanceRemaining - endPoint.distanceRemaining
1001+
val itemDistance = delta / numPointsToCreate
1002+
var lastCalculatedPoint = startPoint
1003+
var distanceRemaining = startPoint.distanceRemaining
1004+
1005+
fillerPoints.add(startPoint)
1006+
repeat(numPointsToCreate) {
1007+
val fillerPoint = TurfMeasurement.destination(
1008+
lastCalculatedPoint.point,
1009+
gapDistanceInMeters,
1010+
bearing,
1011+
TurfConstants.UNIT_METERS
1012+
)
1013+
1014+
distanceRemaining -= itemDistance
1015+
fillerPoints.add(RouteLineDistancesIndex(fillerPoint, distanceRemaining))
1016+
lastCalculatedPoint = fillerPoints.last()
1017+
}
1018+
return fillerPoints
1019+
}
1020+
9581021
private val generateRouteFeatureData: (
9591022
route: NavigationRoute,
9601023
identifier: String?

0 commit comments

Comments
 (0)