Skip to content

Commit 8f6f801

Browse files
tobrungithub-actions[bot]
authored andcommitted
Avoid unnecessary navigation arrow GeoJSON updates (#9738)
Navigation arrows were updating frequently, consuming unnecessary CPU due to frequent GeoJSON source updates. This change checks if the arrow geometry has changed before updating its GeoJSON. GitOrigin-RevId: 17632eff128da688c62e0f1e97dcc3886a31c4ee
1 parent 55abaab commit 8f6f801

File tree

2 files changed

+23
-7
lines changed

2 files changed

+23
-7
lines changed

ui-maps/src/main/java/com/mapbox/navigation/ui/maps/route/arrow/api/MapboxRouteArrowApi.kt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -237,9 +237,7 @@ class MapboxRouteArrowApi {
237237

238238
private fun getShaftFeatureCollection(): FeatureCollection {
239239
val shaftFeatures = arrows.map {
240-
LineString.fromLngLats(it.points)
241-
}.map {
242-
Feature.fromGeometry(it)
240+
Feature.fromGeometry(LineString.fromLngLats(it.points))
243241
}
244242
return FeatureCollection.fromFeatures(shaftFeatures)
245243
}

ui-maps/src/main/java/com/mapbox/navigation/ui/maps/route/arrow/api/MapboxRouteArrowView.kt

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,12 @@ class MapboxRouteArrowView(private val options: RouteArrowOptions) {
4848
private const val LOG_CATEGORY = "MapboxRouteArrowView"
4949
}
5050

51+
/**
52+
* Keep a map of current sources (either [Feature] or [FeatureCollection]) per source id
53+
* (usually [RouteLayerConstants.ARROW_SHAFT_SOURCE_ID] or [RouteLayerConstants.ARROW_HEAD_SOURCE_ID]).
54+
*/
55+
private val currentSourceHashes = mutableMapOf<String, Int>()
56+
5157
/**
5258
* Renders an [ArrowVisibilityChangeValue] applying view side effects based on the data
5359
* it contains.
@@ -195,14 +201,26 @@ class MapboxRouteArrowView(private val options: RouteArrowOptions) {
195201
}
196202

197203
private fun updateSource(style: Style, sourceId: String, feature: Feature) {
198-
if (style.styleSourceExists(sourceId)) {
199-
style.getSourceAs<GeoJsonSource>(sourceId)?.feature(feature)
204+
val newFeatureHash = feature.hashCode()
205+
// Only update the sources if it has changed
206+
if (currentSourceHashes[sourceId] != newFeatureHash) {
207+
val geoJsonSource = style.getSourceAs<GeoJsonSource>(sourceId)
208+
if (geoJsonSource != null) {
209+
geoJsonSource.feature(feature)
210+
currentSourceHashes[sourceId] = newFeatureHash
211+
}
200212
}
201213
}
202214

203215
private fun updateSource(style: Style, sourceId: String, featureCollection: FeatureCollection) {
204-
if (style.styleSourceExists(sourceId)) {
205-
style.getSourceAs<GeoJsonSource>(sourceId)?.featureCollection(featureCollection)
216+
val newFeatureCollectionHash = featureCollection.hashCode()
217+
// Only update the sources if it has changed
218+
if (currentSourceHashes[sourceId] != newFeatureCollectionHash) {
219+
val geoJsonSource = style.getSourceAs<GeoJsonSource>(sourceId)
220+
if (geoJsonSource != null) {
221+
geoJsonSource.featureCollection(featureCollection)
222+
currentSourceHashes[sourceId] = newFeatureCollectionHash
223+
}
206224
}
207225
}
208226

0 commit comments

Comments
 (0)