Skip to content

Commit

Permalink
Fix another bug in LineString distance calculation
Browse files Browse the repository at this point in the history
The iterating over segments was off by one. Add centralized formatting
for the distance strings.
  • Loading branch information
davecraig committed Feb 5, 2025
1 parent 7c3ada7 commit 32e332a
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ class GeoEngine {
if(feature == null) continue
val poiLocation = getDistanceToFeature(userGeometry.location, feature)
val name = getTextForFeature(localizedContext, feature)
val text = "${name.text}. ${localizedContext.getString(R.string.distance_format_meters, poiLocation.distance.toString())}"
val text = "${name.text}. ${formatDistance(poiLocation.distance, localizedContext)}"
list.add(
PositionedString(
text,
Expand Down Expand Up @@ -559,4 +559,14 @@ fun getTextForFeature(localizedContext: Context, feature: Feature) : TextForFeat
}

return TextForFeature(text, generic)
}

fun formatDistance(distance: Double, localizedContext: Context) : String {
if(distance > 1000) {
val km = (distance.toInt() / 100).toFloat() / 10
return localizedContext.getString(R.string.distance_format_km, km.toString())
} else {
val metres = distance.toInt()
return localizedContext.getString(R.string.distance_format_meters, metres.toString())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ open class LngLatAlt(

var shortestDistance = Double.MAX_VALUE
var bestNearestPoint = LngLatAlt()
for(i in 1 until lineStringCoordinates.coordinates.size - 1) {
for(i in 1 until lineStringCoordinates.coordinates.size) {
val nearestPointOnSegment = LngLatAlt()
val distance = distanceToLine(
lineStringCoordinates.coordinates[i-1],
Expand Down

0 comments on commit 32e332a

Please sign in to comment.