From 0995423be64948c41400cd05ae2b6fe5fbb7791b Mon Sep 17 00:00:00 2001 From: Viktor Govako Date: Tue, 6 Feb 2024 13:33:40 -0300 Subject: [PATCH] Fixed negative altitude display. Signed-off-by: Viktor Govako --- .../main/cpp/app/organicmaps/Framework.cpp | 4 ++-- .../java/app/organicmaps/ChartController.java | 4 ++-- .../main/java/app/organicmaps/Framework.java | 2 +- .../java/app/organicmaps/util/Distance.java | 1 + .../widget/placepage/AxisValueFormatter.java | 2 +- .../ElevationProfileViewRenderer.java | 2 +- .../widget/placepage/FloatingMarkerView.java | 2 +- .../widget/placepage/PlacePageView.java | 10 +++----- indexer/map_object.cpp | 2 +- iphone/Maps/Core/Routing/MWMRouter.mm | 4 ++-- .../Layouts/PlacePageCommonLayout.swift | 1 + platform/distance.cpp | 9 ++++--- platform/distance.hpp | 6 ++--- platform/platform_tests/distance_tests.cpp | 24 ++++--------------- 14 files changed, 28 insertions(+), 45 deletions(-) diff --git a/android/app/src/main/cpp/app/organicmaps/Framework.cpp b/android/app/src/main/cpp/app/organicmaps/Framework.cpp index 65c0153c61..891e5a34e9 100644 --- a/android/app/src/main/cpp/app/organicmaps/Framework.cpp +++ b/android/app/src/main/cpp/app/organicmaps/Framework.cpp @@ -1000,10 +1000,10 @@ Java_app_organicmaps_Framework_nativeFormatLatLon(JNIEnv * env, jclass, jdouble } } -JNIEXPORT jobject JNICALL +JNIEXPORT jstring JNICALL Java_app_organicmaps_Framework_nativeFormatAltitude(JNIEnv * env, jclass, jdouble alt) { - return ToJavaDistance(env, platform::Distance::CreateAltitudeFormatted(alt)); + return jni::ToJavaString(env, platform::Distance::FormatAltitude(alt)); } JNIEXPORT jstring JNICALL diff --git a/android/app/src/main/java/app/organicmaps/ChartController.java b/android/app/src/main/java/app/organicmaps/ChartController.java index 664bbfdfe7..89141b7b2e 100644 --- a/android/app/src/main/java/app/organicmaps/ChartController.java +++ b/android/app/src/main/java/app/organicmaps/ChartController.java @@ -176,8 +176,8 @@ public class ChartController implements OnChartValueSelectedListener, mChart.setData(data); mChart.animateX(CHART_ANIMATION_DURATION); - mMinAltitude.setText(Framework.nativeFormatAltitude(info.getMinAltitude()).toString(mContext)); - mMaxAltitude.setText(Framework.nativeFormatAltitude(info.getMaxAltitude()).toString(mContext)); + mMinAltitude.setText(Framework.nativeFormatAltitude(info.getMinAltitude())); + mMaxAltitude.setText(Framework.nativeFormatAltitude(info.getMaxAltitude())); highlightActivePointManually(); } diff --git a/android/app/src/main/java/app/organicmaps/Framework.java b/android/app/src/main/java/app/organicmaps/Framework.java index 5746a6ebe1..039defe45f 100644 --- a/android/app/src/main/java/app/organicmaps/Framework.java +++ b/android/app/src/main/java/app/organicmaps/Framework.java @@ -184,7 +184,7 @@ public class Framework public static native String nativeFormatLatLon(double lat, double lon, int coordFormat); - public static native Distance nativeFormatAltitude(double alt); + public static native String nativeFormatAltitude(double alt); public static native String nativeFormatSpeed(double speed); diff --git a/android/app/src/main/java/app/organicmaps/util/Distance.java b/android/app/src/main/java/app/organicmaps/util/Distance.java index 9b60c662b2..0848fb9bf1 100644 --- a/android/app/src/main/java/app/organicmaps/util/Distance.java +++ b/android/app/src/main/java/app/organicmaps/util/Distance.java @@ -35,6 +35,7 @@ public final class Distance } } + /// @todo What is the difference with cpp: kNarrowNonBreakingSpace = "\u202F" ? private static final char NON_BREAKING_SPACE = '\u00A0'; public final double mDistance; diff --git a/android/app/src/main/java/app/organicmaps/widget/placepage/AxisValueFormatter.java b/android/app/src/main/java/app/organicmaps/widget/placepage/AxisValueFormatter.java index eb6551fab7..a96706f51f 100644 --- a/android/app/src/main/java/app/organicmaps/widget/placepage/AxisValueFormatter.java +++ b/android/app/src/main/java/app/organicmaps/widget/placepage/AxisValueFormatter.java @@ -21,6 +21,6 @@ public class AxisValueFormatter extends DefaultValueFormatter @Override public String getFormattedValue(float value) { - return Framework.nativeFormatAltitude(value).toString(mChart.getContext()); + return Framework.nativeFormatAltitude(value); } } diff --git a/android/app/src/main/java/app/organicmaps/widget/placepage/ElevationProfileViewRenderer.java b/android/app/src/main/java/app/organicmaps/widget/placepage/ElevationProfileViewRenderer.java index 4458e57b02..7a7fba5fdb 100644 --- a/android/app/src/main/java/app/organicmaps/widget/placepage/ElevationProfileViewRenderer.java +++ b/android/app/src/main/java/app/organicmaps/widget/placepage/ElevationProfileViewRenderer.java @@ -82,7 +82,7 @@ public class ElevationProfileViewRenderer implements PlacePageStateListener @NonNull private static String formatDistance(final Context context, int distance) { - return Framework.nativeFormatAltitude(distance).toString(context); + return Framework.nativeFormatAltitude(distance); } public void initialize(@Nullable View view) diff --git a/android/app/src/main/java/app/organicmaps/widget/placepage/FloatingMarkerView.java b/android/app/src/main/java/app/organicmaps/widget/placepage/FloatingMarkerView.java index 5ffe5b5c81..b965b17d46 100644 --- a/android/app/src/main/java/app/organicmaps/widget/placepage/FloatingMarkerView.java +++ b/android/app/src/main/java/app/organicmaps/widget/placepage/FloatingMarkerView.java @@ -185,7 +185,7 @@ public class FloatingMarkerView extends RelativeLayout implements IMarker { mDistanceTextView.setText(R.string.elevation_profile_distance); mDistanceValueView.setText(StringUtils.nativeFormatDistance(entry.getX()).toString(mDistanceValueView.getContext())); - mAltitudeView.setText(Framework.nativeFormatAltitude(entry.getY()).toString(mAltitudeView.getContext())); + mAltitudeView.setText(Framework.nativeFormatAltitude(entry.getY())); } private void updateHorizontal(@NonNull Highlight highlight) diff --git a/android/app/src/main/java/app/organicmaps/widget/placepage/PlacePageView.java b/android/app/src/main/java/app/organicmaps/widget/placepage/PlacePageView.java index 18af67ed35..35badf4543 100644 --- a/android/app/src/main/java/app/organicmaps/widget/placepage/PlacePageView.java +++ b/android/app/src/main/java/app/organicmaps/widget/placepage/PlacePageView.java @@ -446,14 +446,10 @@ public class PlacePageView extends Fragment implements View.OnClickListener, final StringBuilder builder = new StringBuilder(); if (l.hasAltitude()) - { - double altitude = l.getAltitude(); - builder.append(altitude >= 0 ? "▲" : "▼"); - builder.append(Framework.nativeFormatAltitude(altitude).toString(requireContext())); - } + builder.append("▲").append(Framework.nativeFormatAltitude(l.getAltitude())); if (l.hasSpeed()) - builder.append(" ") - .append(Framework.nativeFormatSpeed(l.getSpeed())); + builder.append(" ").append(Framework.nativeFormatSpeed(l.getSpeed())); + UiUtils.setTextAndHideIfEmpty(mTvSubtitle, builder.toString()); mMapObject.setLat(l.getLatitude()); diff --git a/indexer/map_object.cpp b/indexer/map_object.cpp index 2ec297becf..9e57e9b2c6 100644 --- a/indexer/map_object.cpp +++ b/indexer/map_object.cpp @@ -207,7 +207,7 @@ string MapObject::GetElevationFormatted() const { double value; if (strings::to_double(sv, value)) - return platform::Distance::CreateAltitudeFormatted(value).ToString(); + return platform::Distance::FormatAltitude(value); else LOG(LWARNING, ("Invalid elevation metadata:", sv)); } diff --git a/iphone/Maps/Core/Routing/MWMRouter.mm b/iphone/Maps/Core/Routing/MWMRouter.mm index 264767421e..274ee74678 100644 --- a/iphone/Maps/Core/Routing/MWMRouter.mm +++ b/iphone/Maps/Core/Routing/MWMRouter.mm @@ -392,8 +392,8 @@ char const *kRenderAltitudeImagesQueueLabel = "mapsme.mwmrouter.renderAltitudeIm altitudes->CalculateAscentDescent(totalAscentM, totalDescentM); auto const localizedUnits = platform::GetLocalizedAltitudeUnits(); - router.totalAscent = @(platform::Distance::CreateAltitudeFormatted(totalAscentM).ToString().c_str()); - router.totalDescent = @(platform::Distance::CreateAltitudeFormatted(totalDescentM).ToString().c_str()); + router.totalAscent = @(platform::Distance::FormatAltitude(totalAscentM).c_str()); + router.totalDescent = @(platform::Distance::FormatAltitude(totalDescentM).c_str()); } dispatch_async(dispatch_get_main_queue(), ^{ diff --git a/iphone/Maps/UI/PlacePage/PlacePageLayout/Layouts/PlacePageCommonLayout.swift b/iphone/Maps/UI/PlacePage/PlacePageLayout/Layouts/PlacePageCommonLayout.swift index b9da7712c6..2e9ec14f88 100644 --- a/iphone/Maps/UI/PlacePage/PlacePageLayout/Layouts/PlacePageCommonLayout.swift +++ b/iphone/Maps/UI/PlacePage/PlacePageLayout/Layouts/PlacePageCommonLayout.swift @@ -211,6 +211,7 @@ extension PlacePageCommonLayout: MWMLocationObserver { func onLocationUpdate(_ location: CLLocation) { if placePageData.isMyPosition { + /// @todo Use C++ Distance::FormatAltitude function? let imperial = Settings.measurementUnits() == .imperial let alt = imperial ? location.altitude / 0.3048 : location.altitude let altMeasurement = Measurement(value: alt.rounded(), unit: imperial ? UnitLength.feet : UnitLength.meters) diff --git a/platform/distance.cpp b/platform/distance.cpp index 360c8d687f..43eed86639 100644 --- a/platform/distance.cpp +++ b/platform/distance.cpp @@ -82,14 +82,16 @@ Distance Distance::CreateFormatted(double distanceInMeters) return Distance(distanceInMeters).ToPlatformUnitsFormatted(); } -Distance Distance::CreateAltitudeFormatted(double meters) +std::string Distance::FormatAltitude(double meters) { - Distance elevation = Distance(meters).To( + Distance elevation = Distance(fabs(meters)).To( GetMeasurementUnits() == measurement_utils::Units::Metric ? Units::Meters : Units::Feet); ASSERT(elevation.IsLowUnits(), ()); elevation.m_distance = WithPrecision(elevation.m_distance, 0); - return elevation; + + auto res = elevation.ToString(); + return meters < 0 ? "-" + res : res; } bool Distance::IsValid() const { return m_distance >= 0.0; } @@ -207,4 +209,5 @@ std::string DebugPrint(Distance::Units units) default: UNREACHABLE(); } } + } // namespace platform diff --git a/platform/distance.hpp b/platform/distance.hpp index 4fca5dcb9c..f02073b9a7 100644 --- a/platform/distance.hpp +++ b/platform/distance.hpp @@ -31,10 +31,7 @@ public: Distance(double distance, Units units); static Distance CreateFormatted(double distanceInMeters); - /// Creates formatted distance in low units (to display altitude/elevation info). - /// \example CreateAltitudeFormatted(10000) -> 32808 ft - /// \warning GetFormattedDistance() will transform it to high units - static Distance CreateAltitudeFormatted(double meters); + static std::string FormatAltitude(double meters); bool IsValid() const; @@ -66,4 +63,5 @@ private: }; std::string DebugPrint(Distance::Units units); + } // namespace platform diff --git a/platform/platform_tests/distance_tests.cpp b/platform/platform_tests/distance_tests.cpp index ae26fdc8e6..1d3456dd82 100644 --- a/platform/platform_tests/distance_tests.cpp +++ b/platform/platform_tests/distance_tests.cpp @@ -68,38 +68,22 @@ UNIT_TEST(Distance_CreateAltitudeFormatted) { ScopedSettings guard(measurement_utils::Units::Metric); - Distance d = Distance::CreateAltitudeFormatted(5); - TEST_EQUAL(d.GetUnits(), Distance::Units::Meters, ()); - TEST_ALMOST_EQUAL_ULPS(d.GetDistance(), 5.0, ()); - TEST_EQUAL(d.GetDistanceString(), "5", ()); - TEST_EQUAL(d.ToString(), MakeDistanceStr("5", "m"), ()); + TEST_EQUAL(Distance::FormatAltitude(5), MakeDistanceStr("5", "m"), ()); } { ScopedSettings guard(measurement_utils::Units::Metric); - Distance d = Distance::CreateAltitudeFormatted(8849); - TEST_EQUAL(d.GetUnits(), Distance::Units::Meters, ()); - TEST_ALMOST_EQUAL_ULPS(d.GetDistance(), 8849.0, ()); - TEST_EQUAL(d.GetDistanceString(), "8849", ()); - TEST_EQUAL(d.ToString(), MakeDistanceStr("8849", "m"), ()); + TEST_EQUAL(Distance::FormatAltitude(-8849), MakeDistanceStr("-8849", "m"), ()); } { ScopedSettings guard(measurement_utils::Units::Metric); - Distance d = Distance::CreateAltitudeFormatted(12345); - TEST_EQUAL(d.GetUnits(), Distance::Units::Meters, ()); - TEST_ALMOST_EQUAL_ULPS(d.GetDistance(), 12345.0, ()); - TEST_EQUAL(d.GetDistanceString(), "12,345", ()); - TEST_EQUAL(d.ToString(), MakeDistanceStr("12,345", "m"), ()); + TEST_EQUAL(Distance::FormatAltitude(12345), MakeDistanceStr("12,345", "m"), ()); } { ScopedSettings guard(measurement_utils::Units::Imperial); - Distance d = Distance::CreateAltitudeFormatted(10000); - TEST_EQUAL(d.GetUnits(), Distance::Units::Feet, ()); - TEST_ALMOST_EQUAL_ULPS(d.GetDistance(), 32808.0, ()); - TEST_EQUAL(d.GetDistanceString(), "32,808", ()); - TEST_EQUAL(d.ToString(), MakeDistanceStr("32,808", "ft"), ()); + TEST_EQUAL(Distance::FormatAltitude(10000), MakeDistanceStr("32,808", "ft"), ()); } }