Skip to content

Commit dbd52f3

Browse files
author
Seth Bourget
committed
testing for edge case
1 parent 8865baf commit dbd52f3

File tree

3 files changed

+96
-16
lines changed

3 files changed

+96
-16
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
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1503,4 +1503,8 @@ class MapboxRouteLineApi(
15031503
jobControl.scope
15041504
)
15051505
}.cacheResult(alternativelyStyleSegmentsNotInLegCache)
1506+
1507+
fun getFillerPointsInTree(): List<Point> {
1508+
return routeLineOptions.vanishingRouteLine?.getFillerPointsInTree() ?: listOf()
1509+
}
15061510
}

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

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.mapbox.navigation.ui.maps.route.line.api
22

33
import android.graphics.Color
4+
import android.util.Log
45
import android.util.Range
56
import com.mapbox.geojson.Point
67
import com.mapbox.maps.extension.style.expressions.dsl.generated.literal
@@ -20,6 +21,7 @@ import com.mapbox.turf.TurfMeasurement
2021
import kotlinx.coroutines.CoroutineScope
2122
import kotlinx.coroutines.Dispatchers
2223
import kotlinx.coroutines.launch
24+
import java.util.concurrent.CopyOnWriteArrayList
2325

2426
/**
2527
* This class implements a feature that can change the appearance of the route line behind the puck.
@@ -42,9 +44,9 @@ internal class VanishingRouteLine {
4244
private var scope: CoroutineScope? = null
4345
private var granularDistances: RouteLineGranularDistances? = null
4446
private val locationSearchTree = LocationSearchTree<RouteLineDistancesIndex>()
45-
private val fillerPointsInTree = mutableListOf<List<RouteLineDistancesIndex>>()
47+
private val fillerPointsInTree = CopyOnWriteArrayList<RouteLineDistancesIndex>()
4648
private var indexOfLastStepPointsLoadedInTree = 0
47-
private val distanceToLastStepPointInMeters = 30.0
49+
private val distanceToLastStepPointInMeters = 20.0
4850
private var stepPointRange: Range<Int>? = null
4951
private val stepPointRangeSize = 5
5052
private val maxAllowedFillerPointListsInTree = 3
@@ -191,15 +193,16 @@ internal class VanishingRouteLine {
191193
stepPointRange = Range(0, endRange).also {
192194
val fillerPoints = getFillerPointsForRange(it, distances.flatStepDistances)
193195
locationSearchTree.addAll(fillerPoints)
194-
fillerPointsInTree.add(fillerPoints)
196+
fillerPointsInTree.addAll(fillerPoints)
195197
}
196198
}
197199
}
198200
}
199201
}
200202

201203
fun getOffset(point: Point): Double? {
202-
val offset = ifNonNull(locationSearchTree.getNearestNeighbor(point), granularDistances)
204+
val nearestNeighbor = locationSearchTree.getNearestNeighbor(point)
205+
val offset = ifNonNull(nearestNeighbor, granularDistances)
203206
{ closestPoint, distances ->
204207
val distanceBetweenPoints = TurfMeasurement.distance(
205208
point,
@@ -215,7 +218,7 @@ internal class VanishingRouteLine {
215218
null
216219
}
217220
}
218-
trimTree(point)
221+
adjustTree(point, nearestNeighbor)
219222
return offset
220223
}
221224

@@ -235,7 +238,7 @@ internal class VanishingRouteLine {
235238
TurfConstants.UNIT_METERS
236239
)
237240
Pair(index, dist)
238-
}.minByOrNull { it.second }
241+
}.filter { it.first >= indexOfLastStepPointsLoadedInTree }.minByOrNull { it.second }
239242
ifNonNull(indexOfClosestStepPoint) {
240243
val endOfRange =
241244
if (it.first + stepPointRangeSize < distances.flatStepDistances.lastIndex) {
@@ -252,8 +255,9 @@ internal class VanishingRouteLine {
252255
locationSearchTree.clear()
253256
fillerPointsInTree.clear()
254257
locationSearchTree.addAll(fillerPoints)
255-
fillerPointsInTree.add(fillerPoints)
258+
fillerPointsInTree.addAll(fillerPoints)
256259
}
260+
indexOfLastStepPointsLoadedInTree = endOfRange
257261
}
258262
}
259263
}
@@ -277,10 +281,10 @@ internal class VanishingRouteLine {
277281
* the range is redefined with the upcoming points. The points in the newly defined
278282
* range are added to the search tree and the points passed are removed.
279283
*/
280-
private fun trimTree(point: Point) {
284+
private fun adjustTree(point: Point, nearestNeighbor: RouteLineDistancesIndex?) {
281285
scope?.launch(Dispatchers.Main.immediate) {
282-
if (fillerPointsInTree.isNotEmpty() && fillerPointsInTree.last().isNotEmpty()) {
283-
val nearEndStepPoint = fillerPointsInTree.last().last()
286+
if (fillerPointsInTree.isNotEmpty()) {
287+
val nearEndStepPoint = fillerPointsInTree.last()
284288
val distanceToNearEndStepPoint = TurfMeasurement.distance(
285289
point,
286290
nearEndStepPoint.point,
@@ -305,16 +309,32 @@ internal class VanishingRouteLine {
305309
)
306310
if (fillerPoints.isNotEmpty()) {
307311
locationSearchTree.addAll(fillerPoints)
308-
fillerPointsInTree.add(fillerPoints)
312+
fillerPointsInTree.addAll(fillerPoints)
309313
}
314+
indexOfLastStepPointsLoadedInTree = endOfRange
310315
}
311316
}
312-
if (fillerPointsInTree.size == maxAllowedFillerPointListsInTree) {
313-
val pointsToDrop = fillerPointsInTree.removeFirst()
314-
locationSearchTree.removeAll(pointsToDrop)
317+
// if (fillerPointsInTree.size == maxAllowedFillerPointListsInTree) {
318+
// val pointsToDrop = fillerPointsInTree.removeFirst()
319+
// locationSearchTree.removeAll(pointsToDrop)
320+
// }
321+
}
322+
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+
}
315330
}
316331
}
317332
}
318333
}
319334
}
335+
336+
//fixme remove this
337+
fun getFillerPointsInTree(): List<Point> {
338+
return fillerPointsInTree.map { it.point }
339+
}
320340
}

0 commit comments

Comments
 (0)