Skip to content

Commit af1934d

Browse files
author
Seth Bourget
committed
wip fixes and adjustments.
1 parent dbd52f3 commit af1934d

File tree

3 files changed

+94
-46
lines changed

3 files changed

+94
-46
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ class MapboxNavigationActivity : AppCompatActivity() {
236236
// update bottom trip progress summary
237237
binding.tripProgressView.render(tripProgressApi.getTripProgress(routeProgress))
238238

239-
//addPointToPixelMapPoints(routeLineAPI.getFillerPointsInTree())
239+
addPointToPixelMapPoints(routeLineAPI.getFillerPointsInTree())
240240
}
241241

242242
private val routesObserver = RoutesObserver { result ->

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

Lines changed: 36 additions & 0 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
@@ -260,6 +267,8 @@ class MapboxRouteLineAndArrowActivity : AppCompatActivity(), OnMapLongClickListe
260267
// Render the result to update the map.
261268
routeArrowView.renderManeuverUpdate(this, arrowUpdate)
262269
}
270+
271+
//addPointToPixelMapPoints(routeLineApi.getFillerPointsInTree())
263272
}
264273

265274
// RouteLine: Below is a demonstration of selecting different routes. On a map click, a call
@@ -350,6 +359,7 @@ class MapboxRouteLineAndArrowActivity : AppCompatActivity(), OnMapLongClickListe
350359
locationEngineCallback
351360
)
352361
viewBinding.mapView.gestures.addOnMapLongClickListener(this)
362+
initPointLayer(style)
353363
},
354364
object : OnMapLoadErrorListener {
355365
override fun onMapLoadError(eventData: MapLoadingErrorEventData) {
@@ -506,4 +516,30 @@ class MapboxRouteLineAndArrowActivity : AppCompatActivity(), OnMapLongClickListe
506516
override fun onFailure(exception: Exception) {
507517
}
508518
}
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+
}
509545
}

libnavui-maps/src/main/java/com/mapbox/navigation/ui/maps/route/line/api/VanishingRouteLine.kt

Lines changed: 57 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,11 @@ internal class VanishingRouteLine {
4848
private var indexOfLastStepPointsLoadedInTree = 0
4949
private val distanceToLastStepPointInMeters = 20.0
5050
private var stepPointRange: Range<Int>? = null
51-
private val stepPointRangeSize = 5
52-
private val maxAllowedFillerPointListsInTree = 3
51+
private val stepPointRangeSize = 2
52+
//private val maxAllowedFillerPointListsInTree = 3
53+
private val maxTrailingFillerPoints = 10
54+
private val treeAdjustmentFrequency = 10
55+
private var treeAdjustmentCounter = 0
5356

5457
fun setScope(scope: CoroutineScope) {
5558
this.scope = scope
@@ -183,6 +186,7 @@ internal class VanishingRouteLine {
183186
fillerPointsInTree.clear()
184187
indexOfLastStepPointsLoadedInTree = 0
185188
vanishPointOffset = 0.0
189+
treeAdjustmentCounter = 0
186190

187191
if (distances.flatStepDistances.isNotEmpty()) {
188192
val endRange = if (distances.flatStepDistances.size > stepPointRangeSize) {
@@ -257,7 +261,7 @@ internal class VanishingRouteLine {
257261
locationSearchTree.addAll(fillerPoints)
258262
fillerPointsInTree.addAll(fillerPoints)
259263
}
260-
indexOfLastStepPointsLoadedInTree = endOfRange
264+
indexOfLastStepPointsLoadedInTree = range.lower
261265
}
262266
}
263267
}
@@ -282,52 +286,60 @@ internal class VanishingRouteLine {
282286
* range are added to the search tree and the points passed are removed.
283287
*/
284288
private fun adjustTree(point: Point, nearestNeighbor: RouteLineDistancesIndex?) {
285-
scope?.launch(Dispatchers.Main.immediate) {
286-
if (fillerPointsInTree.isNotEmpty()) {
287-
val nearEndStepPoint = fillerPointsInTree.last()
288-
val distanceToNearEndStepPoint = TurfMeasurement.distance(
289-
point,
290-
nearEndStepPoint.point,
291-
TurfConstants.UNIT_METERS
292-
)
293-
if (distanceToNearEndStepPoint <= distanceToLastStepPointInMeters) {
294-
stepPointRange = ifNonNull(stepPointRange, granularDistances)
295-
{ currentStepPointRange, distances ->
296-
val endOfRange =
297-
if (
298-
currentStepPointRange.upper + stepPointRangeSize
299-
< distances.flatStepDistances.lastIndex
300-
) {
301-
currentStepPointRange.upper + stepPointRangeSize
302-
} else {
303-
distances.flatStepDistances.lastIndex
304-
}
305-
Range(currentStepPointRange.upper - 1, endOfRange).also {
306-
val fillerPoints = getFillerPointsForRange(
307-
it,
308-
distances.flatStepDistances
309-
)
310-
if (fillerPoints.isNotEmpty()) {
311-
locationSearchTree.addAll(fillerPoints)
312-
fillerPointsInTree.addAll(fillerPoints)
313-
}
314-
indexOfLastStepPointsLoadedInTree = endOfRange
289+
treeAdjustmentCounter++
290+
if (treeAdjustmentCounter >= treeAdjustmentFrequency) {
291+
treeAdjustmentCounter = 0
292+
scope?.launch(Dispatchers.Main.immediate) {
293+
loadNextRange(point)
294+
trimTree(nearestNeighbor)
295+
}
296+
}
297+
}
298+
299+
private fun loadNextRange(point: Point) {
300+
if (fillerPointsInTree.isNotEmpty()) {
301+
val nearEndStepPoint = fillerPointsInTree.last()
302+
val distanceToNearEndStepPoint = TurfMeasurement.distance(
303+
point,
304+
nearEndStepPoint.point,
305+
TurfConstants.UNIT_METERS
306+
)
307+
if (distanceToNearEndStepPoint <= distanceToLastStepPointInMeters) {
308+
stepPointRange = ifNonNull(stepPointRange, granularDistances)
309+
{ currentStepPointRange, distances ->
310+
val endOfRange =
311+
if (
312+
currentStepPointRange.upper + stepPointRangeSize
313+
< distances.flatStepDistances.lastIndex
314+
) {
315+
currentStepPointRange.upper + stepPointRangeSize
316+
} else {
317+
distances.flatStepDistances.lastIndex
315318
}
319+
Range(currentStepPointRange.upper - 1, endOfRange).also {
320+
val fillerPoints = getFillerPointsForRange(
321+
it,
322+
distances.flatStepDistances
323+
)
324+
if (fillerPoints.isNotEmpty()) {
325+
locationSearchTree.addAll(fillerPoints)
326+
fillerPointsInTree.addAll(fillerPoints)
327+
}
328+
indexOfLastStepPointsLoadedInTree = endOfRange
316329
}
317-
// if (fillerPointsInTree.size == maxAllowedFillerPointListsInTree) {
318-
// val pointsToDrop = fillerPointsInTree.removeFirst()
319-
// locationSearchTree.removeAll(pointsToDrop)
320-
// }
321330
}
331+
}
332+
}
333+
}
322334

323-
ifNonNull(nearestNeighbor) {
324-
val index = fillerPointsInTree.indexOf(it)
325-
if (index > 10) {
326-
fillerPointsInTree.take(index).apply {
327-
fillerPointsInTree.removeAll(this)
328-
locationSearchTree.removeAll(this)
329-
}
330-
}
335+
private fun trimTree(nearestNeighbor: RouteLineDistancesIndex?) {
336+
//Log.e("foobar", "trimming tree")
337+
ifNonNull(nearestNeighbor) {
338+
val index = fillerPointsInTree.indexOf(it)
339+
if (index > maxTrailingFillerPoints) {
340+
fillerPointsInTree.take(index).apply {
341+
fillerPointsInTree.removeAll(this)
342+
locationSearchTree.removeAll(this)
331343
}
332344
}
333345
}

0 commit comments

Comments
 (0)