Skip to content

Commit de2f25e

Browse files
author
Seth Bourget
committed
wip
1 parent 8322e43 commit de2f25e

File tree

6 files changed

+377
-3
lines changed

6 files changed

+377
-3
lines changed

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

Lines changed: 73 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import com.mapbox.maps.EdgeInsets
2222
import com.mapbox.maps.MapboxMap
2323
import com.mapbox.maps.Style
2424
import com.mapbox.maps.extension.observable.eventdata.MapLoadingErrorEventData
25+
import com.mapbox.maps.extension.style.expressions.generated.Expression
2526
import com.mapbox.maps.extension.style.layers.properties.generated.Visibility
2627
import com.mapbox.maps.plugin.LocationPuck2D
2728
import com.mapbox.maps.plugin.animation.MapAnimationOptions
@@ -61,6 +62,7 @@ import com.mapbox.navigation.ui.maps.route.line.model.MapboxRouteLineOptions
6162
import com.mapbox.navigation.ui.maps.route.line.model.RouteLine
6263
import com.mapbox.navigation.ui.maps.route.line.model.RouteLineColorResources
6364
import com.mapbox.navigation.ui.maps.route.line.model.RouteLineResources
65+
import com.mapbox.navigation.ui.maps.route.line.model.RouteLineScaleValue
6466
import java.lang.ref.WeakReference
6567

6668
/**
@@ -213,9 +215,29 @@ class MapboxRouteLineAndArrowActivity : AppCompatActivity(), OnMapLongClickListe
213215
private fun initGradientSelector() {
214216
viewBinding.gradientOptionHard.setOnClickListener {
215217
trafficGradientSoft = false
218+
219+
mapboxMap.getStyle()?.apply {
220+
routeLineView.initializeLayers(this, options)
221+
routeLineApi.getRouteDrawData{
222+
//routeLineView.renderRouteDrawData(this, it)
223+
}
224+
}
216225
}
217226
viewBinding.gradientOptionSoft.setOnClickListener {
218227
trafficGradientSoft = true
228+
229+
val res = routeLineResources.toBuilder()
230+
.routeLineScaleExpression(routeLineScaleExpression)
231+
.routeTrafficLineScaleExpression(routeTrafficLineScaleExpression)
232+
.routeCasingLineScaleExpression(routeCasingLineScaleExpression)
233+
.build()
234+
val updatedOptions = options.toBuilder(this).withRouteLineResources(res).build()
235+
mapboxMap.getStyle()?.apply {
236+
routeLineView.initializeLayers(this, updatedOptions)
237+
routeLineApi.getRouteDrawData{
238+
//routeLineView.renderRouteDrawData(this, it)
239+
}
240+
}
219241
}
220242
}
221243

@@ -375,7 +397,7 @@ class MapboxRouteLineAndArrowActivity : AppCompatActivity(), OnMapLongClickListe
375397
override fun onMapLongClick(point: Point): Boolean {
376398
vibrate()
377399
viewBinding.startNavigation.visibility = View.GONE
378-
viewBinding.optionTrafficGradient.visibility = View.GONE
400+
//viewBinding.optionTrafficGradient.visibility = View.GONE
379401
val currentLocation = navigationLocationProvider.lastLocation
380402
if (currentLocation != null) {
381403
val originPoint = Point.fromLngLat(
@@ -514,4 +536,54 @@ class MapboxRouteLineAndArrowActivity : AppCompatActivity(), OnMapLongClickListe
514536
override fun onFailure(exception: Exception) {
515537
}
516538
}
539+
540+
private val routeTrafficLineScaleExpression: Expression by lazy {
541+
buildScalingExpression(
542+
listOf(
543+
RouteLineScaleValue(4f, 3f, .5f),
544+
RouteLineScaleValue(10f, 4f, .5f),
545+
RouteLineScaleValue(13f, 6f, .5f),
546+
RouteLineScaleValue(16f, 10f, .5f),
547+
RouteLineScaleValue(19f, 14f, .5f),
548+
RouteLineScaleValue(22f, 18f, .5f)
549+
)
550+
)
551+
}
552+
553+
private val routeCasingLineScaleExpression: Expression = buildScalingExpression(
554+
listOf(
555+
RouteLineScaleValue(10f, 7f, 3f),
556+
RouteLineScaleValue(14f, 10.5f, 3f),
557+
RouteLineScaleValue(16.5f, 15.5f, 3f),
558+
RouteLineScaleValue(19f, 24f, 3f),
559+
RouteLineScaleValue(22f, 29f, 3f)
560+
)
561+
)
562+
563+
private val routeLineScaleExpression: Expression = buildScalingExpression(
564+
listOf(
565+
RouteLineScaleValue(4f, 3f, 3f),
566+
RouteLineScaleValue(10f, 4f, 3f),
567+
RouteLineScaleValue(13f, 6f, 3f),
568+
RouteLineScaleValue(16f, 10f, 3f),
569+
RouteLineScaleValue(19f, 14f, 3f),
570+
RouteLineScaleValue(22f, 18f, 3f)
571+
)
572+
)
573+
574+
private fun buildScalingExpression(scalingValues: List<RouteLineScaleValue>): Expression {
575+
val expressionBuilder = Expression.ExpressionBuilder("interpolate")
576+
expressionBuilder.addArgument(Expression.exponential { literal(1.5) })
577+
expressionBuilder.zoom()
578+
scalingValues.forEach { routeLineScaleValue ->
579+
expressionBuilder.stop {
580+
this.literal(routeLineScaleValue.scaleStop.toDouble())
581+
product {
582+
literal(routeLineScaleValue.scaleMultiplier.toDouble())
583+
literal(routeLineScaleValue.scale.toDouble())
584+
}
585+
}
586+
}
587+
return expressionBuilder.build()
588+
}
517589
}

examples/src/main/res/layout/layout_activity_routeline_example.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
android:layout_width="wrap_content"
3131
android:layout_height="wrap_content"
3232
app:layout_constraintStart_toStartOf="parent"
33-
app:layout_constraintBottom_toBottomOf="parent"
33+
app:layout_constraintBottom_toTopOf="@id/startNavigation"
3434
android:orientation="horizontal"
3535
android:background="#ffffff"
3636
android:padding="8dp">

libnavui-maps/src/main/java/com/mapbox/navigation/ui/maps/internal/route/line/MapboxRouteLineUtils.kt

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1751,4 +1751,31 @@ internal object MapboxRouteLineUtils {
17511751
.iconIgnorePlacement(true)
17521752
.iconKeepUpright(true)
17531753
}
1754+
1755+
internal fun removeLayersAndSources(style: Style) {
1756+
style.removeStyleSource(RouteLayerConstants.LAYER_GROUP_1_SOURCE_ID)
1757+
style.removeStyleSource(RouteLayerConstants.LAYER_GROUP_2_SOURCE_ID)
1758+
style.removeStyleSource(RouteLayerConstants.LAYER_GROUP_3_SOURCE_ID)
1759+
style.removeStyleSource(RouteLayerConstants.WAYPOINT_SOURCE_ID)
1760+
style.removeStyleLayer(RouteLayerConstants.TOP_LEVEL_ROUTE_LINE_LAYER_ID)
1761+
style.removeStyleLayer(RouteLayerConstants.BOTTOM_LEVEL_ROUTE_LINE_LAYER_ID)
1762+
style.removeStyleLayer(RouteLayerConstants.LAYER_GROUP_1_TRAIL_CASING)
1763+
style.removeStyleLayer(RouteLayerConstants.LAYER_GROUP_1_TRAIL)
1764+
style.removeStyleLayer(RouteLayerConstants.LAYER_GROUP_1_CASING)
1765+
style.removeStyleLayer(RouteLayerConstants.LAYER_GROUP_1_MAIN)
1766+
style.removeStyleLayer(RouteLayerConstants.LAYER_GROUP_1_TRAFFIC)
1767+
style.removeStyleLayer(RouteLayerConstants.LAYER_GROUP_1_RESTRICTED)
1768+
style.removeStyleLayer(RouteLayerConstants.LAYER_GROUP_2_TRAIL_CASING)
1769+
style.removeStyleLayer(RouteLayerConstants.LAYER_GROUP_2_TRAIL)
1770+
style.removeStyleLayer(RouteLayerConstants.LAYER_GROUP_2_CASING)
1771+
style.removeStyleLayer(RouteLayerConstants.LAYER_GROUP_2_MAIN)
1772+
style.removeStyleLayer(RouteLayerConstants.LAYER_GROUP_2_TRAFFIC)
1773+
style.removeStyleLayer(RouteLayerConstants.LAYER_GROUP_2_RESTRICTED)
1774+
style.removeStyleLayer(RouteLayerConstants.LAYER_GROUP_3_TRAIL_CASING)
1775+
style.removeStyleLayer(RouteLayerConstants.LAYER_GROUP_3_TRAIL)
1776+
style.removeStyleLayer(RouteLayerConstants.LAYER_GROUP_3_CASING)
1777+
style.removeStyleLayer(RouteLayerConstants.LAYER_GROUP_3_MAIN)
1778+
style.removeStyleLayer(RouteLayerConstants.LAYER_GROUP_3_TRAFFIC)
1779+
style.removeStyleLayer(RouteLayerConstants.LAYER_GROUP_3_RESTRICTED)
1780+
}
17541781
}

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

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,14 +142,32 @@ class MapboxRouteLineView(options: MapboxRouteLineOptions) {
142142
* the layers if they have not yet been initialized. If you have a use case for initializing
143143
* the layers in advance of any API calls this method may be used.
144144
*
145+
* If the layers already exist they will be removed and re-initialized with the options provided.
146+
*
145147
* Each [Layer] added to the map by this class is a persistent layer - it will survive style changes.
146148
* This means that if the data has not changed, it does not have to be manually redrawn after a style change.
147149
* See [Style.addPersistentStyleLayer].
148150
*
149151
* @param style a valid [Style] instance
150152
*/
151153
fun initializeLayers(style: Style) {
152-
MapboxRouteLineUtils.initializeLayers(style, options)
154+
resetLayers(style)
155+
}
156+
157+
/**
158+
* Updates the [MapboxRouteLineOptions] used for the route line related layers and initializes the layers.
159+
* If the layers already exist they will be removed and re-initialized with the options provided.
160+
*
161+
* Each [Layer] added to the map by this class is a persistent layer - it will survive style changes.
162+
* This means that if the data has not changed, it does not have to be manually redrawn after a style change.
163+
* See [Style.addPersistentStyleLayer].
164+
*
165+
* @param style a valid [Style] instance
166+
* @param options used for the route line related layers.
167+
*/
168+
fun initializeLayers(style: Style, options: MapboxRouteLineOptions) {
169+
this.options = options
170+
resetLayers(style)
153171
}
154172

155173
/**
@@ -862,4 +880,26 @@ class MapboxRouteLineView(options: MapboxRouteLineOptions) {
862880
}
863881
}
864882
}
883+
884+
private fun resetLayers(style: Style) {
885+
sourceToFeatureMap.clear()
886+
sourceToFeatureMap[MapboxRouteLineUtils.layerGroup1SourceKey] = RouteLineFeatureId(null)
887+
sourceToFeatureMap[MapboxRouteLineUtils.layerGroup2SourceKey] = RouteLineFeatureId(null)
888+
sourceToFeatureMap[MapboxRouteLineUtils.layerGroup3SourceKey] = RouteLineFeatureId(null)
889+
primaryRouteLineLayerGroup = setOf()
890+
listOf(
891+
RouteLayerConstants.LAYER_GROUP_1_SOURCE_ID,
892+
RouteLayerConstants.LAYER_GROUP_2_SOURCE_ID,
893+
RouteLayerConstants.LAYER_GROUP_3_SOURCE_ID,
894+
RouteLayerConstants.WAYPOINT_SOURCE_ID
895+
).forEach {
896+
updateSource(
897+
style,
898+
it,
899+
FeatureCollection.fromFeatures(listOf())
900+
)
901+
}
902+
MapboxRouteLineUtils.removeLayersAndSources(style)
903+
MapboxRouteLineUtils.initializeLayers(style, options)
904+
}
865905
}

libnavui-maps/src/test/java/com/mapbox/navigation/ui/maps/internal/route/line/MapboxRouteLineUtilsTest.kt

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2028,6 +2028,41 @@ class MapboxRouteLineUtilsTest {
20282028
assertFalse(result)
20292029
}
20302030

2031+
@Test
2032+
fun removeLayersAndSources() {
2033+
val style = mockk<Style> {
2034+
every { removeStyleLayer(any()) } returns ExpectedFactory.createNone()
2035+
every { removeStyleSource(any()) } returns ExpectedFactory.createNone()
2036+
}
2037+
2038+
MapboxRouteLineUtils.removeLayersAndSources(style)
2039+
2040+
verify { style.removeStyleSource(LAYER_GROUP_1_SOURCE_ID) }
2041+
verify { style.removeStyleSource(LAYER_GROUP_2_SOURCE_ID) }
2042+
verify { style.removeStyleSource(LAYER_GROUP_3_SOURCE_ID) }
2043+
verify { style.removeStyleSource(WAYPOINT_SOURCE_ID) }
2044+
verify { style.removeStyleLayer(TOP_LEVEL_ROUTE_LINE_LAYER_ID) }
2045+
verify { style.removeStyleLayer(BOTTOM_LEVEL_ROUTE_LINE_LAYER_ID) }
2046+
verify { style.removeStyleLayer(LAYER_GROUP_1_TRAIL_CASING) }
2047+
verify { style.removeStyleLayer(LAYER_GROUP_1_TRAIL) }
2048+
verify { style.removeStyleLayer(LAYER_GROUP_1_CASING) }
2049+
verify { style.removeStyleLayer(LAYER_GROUP_1_MAIN) }
2050+
verify { style.removeStyleLayer(LAYER_GROUP_1_TRAFFIC) }
2051+
verify { style.removeStyleLayer(LAYER_GROUP_1_RESTRICTED) }
2052+
verify { style.removeStyleLayer(LAYER_GROUP_2_TRAIL_CASING) }
2053+
verify { style.removeStyleLayer(LAYER_GROUP_2_TRAIL) }
2054+
verify { style.removeStyleLayer(LAYER_GROUP_2_CASING) }
2055+
verify { style.removeStyleLayer(LAYER_GROUP_2_MAIN) }
2056+
verify { style.removeStyleLayer(LAYER_GROUP_2_TRAFFIC) }
2057+
verify { style.removeStyleLayer(LAYER_GROUP_2_RESTRICTED) }
2058+
verify { style.removeStyleLayer(LAYER_GROUP_3_TRAIL_CASING) }
2059+
verify { style.removeStyleLayer(LAYER_GROUP_3_TRAIL) }
2060+
verify { style.removeStyleLayer(LAYER_GROUP_3_CASING) }
2061+
verify { style.removeStyleLayer(LAYER_GROUP_3_MAIN) }
2062+
verify { style.removeStyleLayer(LAYER_GROUP_3_TRAFFIC) }
2063+
verify { style.removeStyleLayer(LAYER_GROUP_3_RESTRICTED) }
2064+
}
2065+
20312066
private fun <T> listElementsAreEqual(
20322067
first: List<T>,
20332068
second: List<T>,

0 commit comments

Comments
 (0)