@@ -18,6 +18,7 @@ import com.mapbox.navigation.ui.maps.internal.route.line.MapboxRouteLineUtils.ge
18
18
import com.mapbox.navigation.ui.maps.internal.route.line.MapboxRouteLineUtils.layerGroup1SourceLayerIds
19
19
import com.mapbox.navigation.ui.maps.internal.route.line.MapboxRouteLineUtils.layerGroup2SourceLayerIds
20
20
import com.mapbox.navigation.ui.maps.internal.route.line.MapboxRouteLineUtils.layerGroup3SourceLayerIds
21
+ import com.mapbox.navigation.ui.maps.internal.route.line.MapboxRouteLineUtils.layersAreInitialized
21
22
import com.mapbox.navigation.ui.maps.internal.route.line.MapboxRouteLineUtils.sourceLayerMap
22
23
import com.mapbox.navigation.ui.maps.route.RouteLayerConstants
23
24
import com.mapbox.navigation.ui.maps.route.RouteLayerConstants.LAYER_GROUP_1_CASING
@@ -78,6 +79,8 @@ import org.jetbrains.annotations.TestOnly
78
79
@UiThread
79
80
class MapboxRouteLineView (options : MapboxRouteLineOptions ) {
80
81
82
+ private var rebuildLayersOnFirstRender: Boolean = true
83
+
81
84
private companion object {
82
85
private const val TAG = " MbxRouteLineView"
83
86
}
@@ -142,14 +145,32 @@ class MapboxRouteLineView(options: MapboxRouteLineOptions) {
142
145
* the layers if they have not yet been initialized. If you have a use case for initializing
143
146
* the layers in advance of any API calls this method may be used.
144
147
*
148
+ * If the layers already exist they will be removed and re-initialized with the options provided.
149
+ *
145
150
* Each [Layer] added to the map by this class is a persistent layer - it will survive style changes.
146
151
* This means that if the data has not changed, it does not have to be manually redrawn after a style change.
147
152
* See [Style.addPersistentStyleLayer].
148
153
*
149
154
* @param style a valid [Style] instance
150
155
*/
151
156
fun initializeLayers (style : Style ) {
152
- MapboxRouteLineUtils .initializeLayers(style, options)
157
+ resetLayers(style)
158
+ }
159
+
160
+ /* *
161
+ * Updates the [MapboxRouteLineOptions] used for the route line related layers and initializes the layers.
162
+ * If the layers already exist they will be removed and re-initialized with the options provided.
163
+ *
164
+ * Each [Layer] added to the map by this class is a persistent layer - it will survive style changes.
165
+ * This means that if the data has not changed, it does not have to be manually redrawn after a style change.
166
+ * See [Style.addPersistentStyleLayer].
167
+ *
168
+ * @param style a valid [Style] instance
169
+ * @param options used for the route line related layers.
170
+ */
171
+ fun initializeLayers (style : Style , options : MapboxRouteLineOptions ) {
172
+ this .options = options
173
+ resetLayers(style)
153
174
}
154
175
155
176
/* *
@@ -159,7 +180,7 @@ class MapboxRouteLineView(options: MapboxRouteLineOptions) {
159
180
* @param routeDrawData a [Expected<RouteLineError, RouteSetValue>]
160
181
*/
161
182
fun renderRouteDrawData (style : Style , routeDrawData : Expected <RouteLineError , RouteSetValue >) {
162
- MapboxRouteLineUtils .initializeLayers (style, options )
183
+ rebuildSourcesAndLayersIfNeeded (style)
163
184
jobControl.scope.launch(Dispatchers .Main ) {
164
185
mutex.withLock {
165
186
val primaryRouteTrafficVisibility = getTrafficVisibility(style)
@@ -332,7 +353,7 @@ class MapboxRouteLineView(options: MapboxRouteLineOptions) {
332
353
style : Style ,
333
354
clearRouteLineValue : Expected <RouteLineError , RouteLineClearValue >
334
355
) {
335
- MapboxRouteLineUtils .initializeLayers (style, options )
356
+ rebuildSourcesAndLayersIfNeeded (style)
336
357
jobControl.scope.launch(Dispatchers .Main ) {
337
358
mutex.withLock {
338
359
clearRouteLineValue.onValue { value ->
@@ -862,4 +883,33 @@ class MapboxRouteLineView(options: MapboxRouteLineOptions) {
862
883
}
863
884
}
864
885
}
886
+
887
+ private fun rebuildSourcesAndLayersIfNeeded (style : Style ) {
888
+ if (rebuildLayersOnFirstRender || ! layersAreInitialized(style, options)) {
889
+ rebuildLayersOnFirstRender = false
890
+ resetLayers(style)
891
+ }
892
+ }
893
+
894
+ private fun resetLayers (style : Style ) {
895
+ sourceToFeatureMap.clear()
896
+ sourceToFeatureMap[MapboxRouteLineUtils .layerGroup1SourceKey] = RouteLineFeatureId (null )
897
+ sourceToFeatureMap[MapboxRouteLineUtils .layerGroup2SourceKey] = RouteLineFeatureId (null )
898
+ sourceToFeatureMap[MapboxRouteLineUtils .layerGroup3SourceKey] = RouteLineFeatureId (null )
899
+ primaryRouteLineLayerGroup = setOf ()
900
+ listOf (
901
+ RouteLayerConstants .LAYER_GROUP_1_SOURCE_ID ,
902
+ RouteLayerConstants .LAYER_GROUP_2_SOURCE_ID ,
903
+ RouteLayerConstants .LAYER_GROUP_3_SOURCE_ID ,
904
+ RouteLayerConstants .WAYPOINT_SOURCE_ID
905
+ ).forEach {
906
+ updateSource(
907
+ style,
908
+ it,
909
+ FeatureCollection .fromFeatures(listOf ())
910
+ )
911
+ }
912
+ MapboxRouteLineUtils .removeLayersAndSources(style)
913
+ MapboxRouteLineUtils .initializeLayers(style, options)
914
+ }
865
915
}
0 commit comments