@@ -18,6 +18,7 @@ import com.mapbox.navigation.ui.maps.internal.route.line.MapboxRouteLineUtils.ge
1818import com.mapbox.navigation.ui.maps.internal.route.line.MapboxRouteLineUtils.layerGroup1SourceLayerIds
1919import com.mapbox.navigation.ui.maps.internal.route.line.MapboxRouteLineUtils.layerGroup2SourceLayerIds
2020import com.mapbox.navigation.ui.maps.internal.route.line.MapboxRouteLineUtils.layerGroup3SourceLayerIds
21+ import com.mapbox.navigation.ui.maps.internal.route.line.MapboxRouteLineUtils.layersAreInitialized
2122import com.mapbox.navigation.ui.maps.internal.route.line.MapboxRouteLineUtils.sourceLayerMap
2223import com.mapbox.navigation.ui.maps.route.RouteLayerConstants
2324import com.mapbox.navigation.ui.maps.route.RouteLayerConstants.LAYER_GROUP_1_CASING
@@ -78,6 +79,8 @@ import org.jetbrains.annotations.TestOnly
7879@UiThread
7980class MapboxRouteLineView (options : MapboxRouteLineOptions ) {
8081
82+ private var rebuildLayersOnFirstRender: Boolean = true
83+
8184 private companion object {
8285 private const val TAG = " MbxRouteLineView"
8386 }
@@ -142,14 +145,32 @@ class MapboxRouteLineView(options: MapboxRouteLineOptions) {
142145 * the layers if they have not yet been initialized. If you have a use case for initializing
143146 * the layers in advance of any API calls this method may be used.
144147 *
148+ * If the layers already exist they will be removed and re-initialized with the options provided.
149+ *
145150 * Each [Layer] added to the map by this class is a persistent layer - it will survive style changes.
146151 * This means that if the data has not changed, it does not have to be manually redrawn after a style change.
147152 * See [Style.addPersistentStyleLayer].
148153 *
149154 * @param style a valid [Style] instance
150155 */
151156 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)
153174 }
154175
155176 /* *
@@ -159,7 +180,7 @@ class MapboxRouteLineView(options: MapboxRouteLineOptions) {
159180 * @param routeDrawData a [Expected<RouteLineError, RouteSetValue>]
160181 */
161182 fun renderRouteDrawData (style : Style , routeDrawData : Expected <RouteLineError , RouteSetValue >) {
162- MapboxRouteLineUtils .initializeLayers (style, options )
183+ rebuildSourcesAndLayersIfNeeded (style)
163184 jobControl.scope.launch(Dispatchers .Main ) {
164185 mutex.withLock {
165186 val primaryRouteTrafficVisibility = getTrafficVisibility(style)
@@ -332,7 +353,7 @@ class MapboxRouteLineView(options: MapboxRouteLineOptions) {
332353 style : Style ,
333354 clearRouteLineValue : Expected <RouteLineError , RouteLineClearValue >
334355 ) {
335- MapboxRouteLineUtils .initializeLayers (style, options )
356+ rebuildSourcesAndLayersIfNeeded (style)
336357 jobControl.scope.launch(Dispatchers .Main ) {
337358 mutex.withLock {
338359 clearRouteLineValue.onValue { value ->
@@ -862,4 +883,33 @@ class MapboxRouteLineView(options: MapboxRouteLineOptions) {
862883 }
863884 }
864885 }
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+ }
865915}
0 commit comments