Skip to content

Commit 8e242f5

Browse files
DSamaryangithub-actions[bot]
authored andcommitted
Added geometryPointAt extension on RouteLeg (#13133)
CP mapbox/navigation#13039 GitOrigin-RevId: c7f70dfdb3cb6f921c0ba70fc6575395165ddaae
1 parent 3e1a62e commit 8e242f5

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

changelog/new-changes.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
- Added `geometryPointAt(index, precision)` extension on `RouteLeg` that decodes leg geometry one step at a time and stops at the target index, avoiding allocation of all subsequent points.

utils/src/main/java/com/mapbox/navigation/utils/internal/RouteEx.kt

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,31 @@ fun RouteLeg.geometryPoints(precision: Int): List<Point> {
2424
}
2525
}
2626

27+
/**
28+
* Returns the [Point] at [index] in the leg's geometry without decoding all steps.
29+
* Decodes one step at a time and stops as soon as the target index is reached.
30+
*/
31+
fun RouteLeg.geometryPointAt(index: Int, precision: Int): Point? {
32+
var remaining = index
33+
val stepList = steps() ?: return null
34+
for (i in stepList.indices) {
35+
val geometry = stepList[i].geometry() ?: continue
36+
val points = PolylineUtils.decode(geometry, precision)
37+
// Mirror geometryPoints() deduplication logic:
38+
// - First step: contributes all points (or unique set if size == 2)
39+
// - Subsequent steps: first point is shared with previous step's last, skip it
40+
val skip = if (i == 0) 0 else 1
41+
val effective = if (points.size == 2) {
42+
if (points[0] == points[1]) maxOf(1 - skip, 0) else (2 - skip)
43+
} else {
44+
points.size - skip
45+
}
46+
if (remaining < effective) return points[skip + remaining]
47+
remaining -= effective
48+
}
49+
return null
50+
}
51+
2752
fun RouteLeg.stepsGeometryToPoints(precision: Int): List<List<Point>> {
2853
return steps()?.map { step ->
2954
val geometry = step.geometry() ?: return@map emptyList()

0 commit comments

Comments
 (0)