@@ -39,35 +39,10 @@ class CarRouteLine(
39
39
.build()
40
40
}
41
41
42
- private val options: MapboxRouteLineOptions by lazy {
43
- MapboxRouteLineOptions .Builder (mainCarContext.carContext)
44
- .withRouteLineResources(routeLineResources)
45
- .withRouteLineBelowLayerId(" road-label" )
46
- .withVanishingRouteLineEnabled(true )
47
- .build()
48
- }
49
-
50
- private val routeLineView by lazy {
51
- MapboxRouteLineView (options)
52
- }
53
-
54
- private val routeLineApi: MapboxRouteLineApi by lazy {
55
- MapboxRouteLineApi (options)
56
- }
57
-
58
- private val routeArrowApi: MapboxRouteArrowApi by lazy {
59
- MapboxRouteArrowApi ()
60
- }
61
-
62
- private val routeArrowOptions by lazy {
63
- RouteArrowOptions .Builder (mainCarContext.carContext)
64
- .withAboveLayerId(TOP_LEVEL_ROUTE_LINE_LAYER_ID )
65
- .build()
66
- }
67
-
68
- private val routeArrowView: MapboxRouteArrowView by lazy {
69
- MapboxRouteArrowView (routeArrowOptions)
70
- }
42
+ private lateinit var routeLineView: MapboxRouteLineView
43
+ private lateinit var routeLineApi: MapboxRouteLineApi
44
+ private lateinit var routeArrowApi: MapboxRouteArrowApi
45
+ private lateinit var routeArrowView: MapboxRouteArrowView
71
46
72
47
private val onPositionChangedListener = OnIndicatorPositionChangedListener { point ->
73
48
val result = routeLineApi.updateTraveledRouteLine(point)
@@ -107,12 +82,23 @@ class CarRouteLine(
107
82
override fun loaded (mapboxCarMapSurface : MapboxCarMapSurface ) {
108
83
logAndroidAuto(" CarRouteLine carMapSurface loaded $mapboxCarMapSurface " )
109
84
val locationPlugin = mapboxCarMapSurface.mapSurface.location
110
- routeLineView.initializeLayers(mapboxCarMapSurface.style)
85
+
86
+ val routeLineOptions = getMapboxRouteLineOptions(mapboxCarMapSurface)
87
+ routeLineView = MapboxRouteLineView (routeLineOptions)
88
+ routeLineApi = MapboxRouteLineApi (routeLineOptions).also {
89
+ routeLineView.initializeLayers(mapboxCarMapSurface.style)
90
+ }
91
+ routeArrowApi = MapboxRouteArrowApi ()
92
+ routeArrowView = MapboxRouteArrowView (
93
+ RouteArrowOptions .Builder (mainCarContext.carContext)
94
+ .withAboveLayerId(TOP_LEVEL_ROUTE_LINE_LAYER_ID )
95
+ .build()
96
+ )
97
+
111
98
locationPlugin.addOnIndicatorPositionChangedListener(onPositionChangedListener)
112
99
mainCarContext.mapboxNavigation.apply {
113
100
registerRouteProgressObserver(routeProgressObserver)
114
101
registerRoutesObserver(routesObserver)
115
- historyRecorder.startRecording()
116
102
}
117
103
}
118
104
@@ -124,12 +110,21 @@ class CarRouteLine(
124
110
unregisterRouteProgressObserver(routeProgressObserver)
125
111
unregisterRoutesObserver(routesObserver)
126
112
}
113
+ }
127
114
128
- mainCarContext.mapboxNavigation.historyRecorder.stopRecording {
129
- logAndroidAuto(" CarRouteLine saved history $it " )
130
- }
115
+ private fun getMapboxRouteLineOptions (
116
+ mapboxCarMapSurface : MapboxCarMapSurface
117
+ ): MapboxRouteLineOptions {
118
+ return MapboxRouteLineOptions .Builder (mainCarContext.carContext)
119
+ .withRouteLineResources(routeLineResources)
120
+ .withRouteLineBelowLayerId(findRoadLabelsLayerId(mapboxCarMapSurface))
121
+ .withVanishingRouteLineEnabled(true )
122
+ .build()
123
+ }
131
124
132
- routeLineApi.cancel()
133
- routeLineView.cancel()
125
+ private fun findRoadLabelsLayerId (mapboxCarMapSurface : MapboxCarMapSurface ): String {
126
+ return mapboxCarMapSurface.style.styleLayers.firstOrNull { layer ->
127
+ layer.id.contains(" road-label" )
128
+ }?.id ? : " road-label-navigation"
134
129
}
135
130
}
0 commit comments