Skip to content

Commit 4fa9622

Browse files
committed
NAVAND-979: represent imperial distances in miles down to 0.1 miles in miles
1 parent 8322e43 commit 4fa9622

File tree

3 files changed

+372
-36
lines changed

3 files changed

+372
-36
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ Mapbox welcomes participation and contributions from everyone.
2929
- Introduced `NavigationViewListener.onSpeedInfoClicked` that would be triggered when `MapboxSpeedInfoView` is clicked upon. [#6770](https://github.com/mapbox/mapbox-navigation-android/pull/6770)
3030
- Each newly instantiated MapboxRouteArrowView class will initialize the layers with the provided options on the first render call. Previously this would only be done if the layers hadn't already been initialized. [#6466](https://github.com/mapbox/mapbox-navigation-android/pull/6466)
3131
- Fixed an issue where the first voice instruction might have been played twice. [#6766](https://github.com/mapbox/mapbox-navigation-android/pull/6766)
32+
- Changed distance formatting: now all the imperial distances down to 0.1 miles will be represented in miles, while the smaller ones - in feet. [#6775](https://github.com/mapbox/mapbox-navigation-android/pull/6775)
3233

3334
## Mapbox Navigation SDK 2.10.0-rc.1 - 16 December, 2022
3435
### Changelog

libnavigation-core/src/main/java/com/mapbox/navigation/core/formatter/MapboxDistanceUtil.kt

+76-32
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,10 @@ import kotlin.math.roundToInt
1616
*/
1717
object MapboxDistanceUtil {
1818

19-
private const val smallDistanceUpperThresholdInMeters = 400.0
20-
private const val mediumDistanceUpperThresholdInMeters = 10000.0
19+
private const val SMALL_DISTANCE_LOWER_BOUND_IN_MILES = 0.1
20+
private const val MEDIUM_DISTANCE_LOWER_BOUND_IN_MILES = 10.0
21+
private const val SMALL_DISTANCE_UPPER_THRESHOLD_IN_METERS = 400.0
22+
private const val MEDIUM_DISTANCE_UPPER_THRESHOLD_IN_METERS = 10000.0
2123

2224
/**
2325
* This will recalculate and format a distance based on the parameters inputted. The value
@@ -37,31 +39,35 @@ object MapboxDistanceUtil {
3739
context: Context,
3840
locale: Locale
3941
): FormattedDistanceData {
40-
return when (distanceInMeters) {
41-
!in 0.0..Double.MAX_VALUE -> {
42-
formatDistanceAndSuffixForSmallUnit(
43-
0.0,
44-
roundingIncrement,
45-
unitType,
46-
context,
47-
locale
48-
)
49-
}
50-
in 0.0..smallDistanceUpperThresholdInMeters -> {
51-
formatDistanceAndSuffixForSmallUnit(
52-
distanceInMeters,
53-
roundingIncrement,
54-
unitType,
55-
context,
56-
locale
57-
)
58-
}
59-
in smallDistanceUpperThresholdInMeters..mediumDistanceUpperThresholdInMeters -> {
60-
formatDistanceAndSuffixForLargeUnit(distanceInMeters, 1, unitType, context, locale)
61-
}
62-
else -> {
63-
formatDistanceAndSuffixForLargeUnit(distanceInMeters, 0, unitType, context, locale)
64-
}
42+
return when (getFormattingRange(distanceInMeters, unitType)) {
43+
FormattingRange.INVALID -> formatDistanceAndSuffixForSmallUnit(
44+
0.0,
45+
roundingIncrement,
46+
unitType,
47+
context,
48+
locale
49+
)
50+
FormattingRange.SMALL -> formatDistanceAndSuffixForSmallUnit(
51+
distanceInMeters,
52+
roundingIncrement,
53+
unitType,
54+
context,
55+
locale
56+
)
57+
FormattingRange.MEDIUM -> formatDistanceAndSuffixForLargeUnit(
58+
distanceInMeters,
59+
1,
60+
unitType,
61+
context,
62+
locale
63+
)
64+
FormattingRange.LARGE -> formatDistanceAndSuffixForLargeUnit(
65+
distanceInMeters,
66+
0,
67+
unitType,
68+
context,
69+
locale
70+
)
6571
}
6672
}
6773

@@ -104,17 +110,17 @@ object MapboxDistanceUtil {
104110
roundingIncrement: Int,
105111
unitType: UnitType
106112
): Double {
107-
return when (distanceInMeters) {
108-
!in 0.0..Double.MAX_VALUE -> {
113+
return when (getFormattingRange(distanceInMeters, unitType)) {
114+
FormattingRange.INVALID -> {
109115
roundSmallDistance(distanceInMeters, roundingIncrement, unitType).toDouble()
110116
}
111-
in 0.0..smallDistanceUpperThresholdInMeters -> {
117+
FormattingRange.SMALL -> {
112118
roundSmallDistance(distanceInMeters, roundingIncrement, unitType).toDouble()
113119
}
114-
in smallDistanceUpperThresholdInMeters..mediumDistanceUpperThresholdInMeters -> {
120+
FormattingRange.MEDIUM -> {
115121
roundLargeDistance(distanceInMeters, unitType)
116122
}
117-
else -> {
123+
FormattingRange.LARGE -> {
118124
roundLargeDistance(distanceInMeters, unitType)
119125
}
120126
}
@@ -234,4 +240,42 @@ object MapboxDistanceUtil {
234240
}
235241
return this.createConfigurationContext(config).resources
236242
}
243+
244+
private enum class FormattingRange { INVALID, SMALL, MEDIUM, LARGE }
245+
246+
private fun getFormattingRange(
247+
distanceInMeters: Double,
248+
unitType: UnitType
249+
): FormattingRange {
250+
if (distanceInMeters !in 0.0..Double.MAX_VALUE) {
251+
return FormattingRange.INVALID
252+
}
253+
return when (unitType) {
254+
UnitType.METRIC -> metricRange(distanceInMeters)
255+
UnitType.IMPERIAL -> {
256+
val distanceInMiles = TurfConversion.convertLength(
257+
distanceInMeters,
258+
TurfConstants.UNIT_METERS,
259+
getLargeTurfUnitType(unitType)
260+
)
261+
imperialRange(distanceInMiles)
262+
}
263+
}
264+
}
265+
266+
private fun metricRange(distance: Double): FormattingRange = when (distance) {
267+
in 0.0..SMALL_DISTANCE_UPPER_THRESHOLD_IN_METERS -> FormattingRange.SMALL
268+
in SMALL_DISTANCE_UPPER_THRESHOLD_IN_METERS..MEDIUM_DISTANCE_UPPER_THRESHOLD_IN_METERS ->
269+
FormattingRange.MEDIUM
270+
else -> FormattingRange.LARGE
271+
}
272+
273+
private fun imperialRange(distance: Double): FormattingRange {
274+
return when (distance) {
275+
in 0.0..SMALL_DISTANCE_LOWER_BOUND_IN_MILES -> FormattingRange.SMALL
276+
in SMALL_DISTANCE_LOWER_BOUND_IN_MILES..MEDIUM_DISTANCE_LOWER_BOUND_IN_MILES ->
277+
FormattingRange.MEDIUM
278+
else -> FormattingRange.LARGE
279+
}
280+
}
237281
}

0 commit comments

Comments
 (0)