From c44da1728ad73d4dfe324d050de2694f707ebb60 Mon Sep 17 00:00:00 2001 From: Chumva Date: Tue, 10 Dec 2019 13:40:43 +0200 Subject: [PATCH] Parse speed in location messages --- .../telegram/utils/OsmandLocationUtils.kt | 26 ++++++++++++++----- 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/OsmAnd-telegram/src/net/osmand/telegram/utils/OsmandLocationUtils.kt b/OsmAnd-telegram/src/net/osmand/telegram/utils/OsmandLocationUtils.kt index 59e69508e5..8e418ac0e0 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/utils/OsmandLocationUtils.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/utils/OsmandLocationUtils.kt @@ -256,12 +256,7 @@ object OsmandLocationUtils { } s.startsWith(SPEED_PREFIX) -> { val speedStr = s.removePrefix(SPEED_PREFIX) - try { - val speed = speedStr.split(" ").first() - res.speed = speed.toDouble() - } catch (e: Exception) { - e.printStackTrace() - } + res.speed = parseSpeed(speedStr) } s.startsWith(BEARING_PREFIX) -> { val bearingStr = s.removePrefix(BEARING_PREFIX) @@ -335,6 +330,25 @@ object OsmandLocationUtils { return 0 } + fun parseSpeed(speedS: String): Double { + try { + val speedSplit = speedS.split(" ") + val speedVal = speedSplit.first().toDouble() + val speedFormat = OsmandFormatter.SpeedConstants.values().firstOrNull { it.getDefaultString() == speedSplit.last() } + return when (speedFormat) { + OsmandFormatter.SpeedConstants.KILOMETERS_PER_HOUR -> speedVal / 3.6f + OsmandFormatter.SpeedConstants.MILES_PER_HOUR -> (speedVal / 3.6f) / (OsmandFormatter.METERS_IN_KILOMETER / OsmandFormatter.METERS_IN_ONE_MILE) + OsmandFormatter.SpeedConstants.NAUTICALMILES_PER_HOUR -> (speedVal / 3.6f) / (OsmandFormatter.METERS_IN_KILOMETER / OsmandFormatter.METERS_IN_ONE_NAUTICALMILE) + OsmandFormatter.SpeedConstants.MINUTES_PER_KILOMETER -> (OsmandFormatter.METERS_IN_KILOMETER / speedVal) / 60 + OsmandFormatter.SpeedConstants.MINUTES_PER_MILE -> (OsmandFormatter.METERS_IN_ONE_MILE / speedVal) / 60 + else -> speedVal + } + } catch (e: Exception) { + e.printStackTrace() + } + return 0.0 + } + fun parseDistance(distanceS: String): Double { try { val distanceSplit = distanceS.split(" ")