diff --git a/Source/GnssTracker_Demo/Controllers/DisplayController.cs b/Source/GnssTracker_Demo/Controllers/DisplayController.cs index a9a0382..b659119 100644 --- a/Source/GnssTracker_Demo/Controllers/DisplayController.cs +++ b/Source/GnssTracker_Demo/Controllers/DisplayController.cs @@ -4,6 +4,7 @@ using Meadow.Peripherals.Displays; using Meadow.Peripherals.Sensors.Location.Gnss; using Meadow.Units; +using System; namespace GnssTracker_Demo.Controllers; @@ -196,22 +197,32 @@ public void UpdateDisplay( co2LevelsLabel.Text = $"{Concentration?.PartsPerMillion:N1} PPM"; } - string lat = locationInfo == null - ? $"00 00' 0.00\"" - : $"" + - $"{locationInfo?.Position?.Latitude?.Degrees:N2} " + - $"{locationInfo?.Position?.Latitude?.Minutes:N2}'" + - $"{locationInfo?.Position?.Latitude?.Seconds:N2}\""; + var geo = new GeoLocation(locationInfo?.Position?.Latitude ?? 0, locationInfo?.Position?.Longitude ?? 0); + + string lat = ConvertToDMS(geo.Latitude); latitudeLabel.Text = lat; - string lon = locationInfo == null - ? $"00 00' 0.00\"" - : $"" + - $"{locationInfo?.Position?.Longitude?.Degrees:N2} " + - $"{locationInfo?.Position?.Longitude?.Minutes:N2}'" + - $"{locationInfo?.Position?.Longitude?.Seconds:N2}\""; + string lon = ConvertToDMS(geo.Longitude); longitudeLabel.Text = lon; displayScreen.EndUpdate(); } + + public string ConvertToDMS(double decimalDegrees) + { + bool isNegative = decimalDegrees < 0; + decimalDegrees = Math.Abs(decimalDegrees); + + int degrees = (int)decimalDegrees; + + double fractionalPart = decimalDegrees - degrees; + double totalMinutes = fractionalPart * 60; + int minutes = (int)totalMinutes; + + double seconds = (totalMinutes - minutes) * 60; + + string dms = $"{degrees}° {minutes}' {seconds:F2}\""; + + return isNegative ? "-" + dms : dms; + } } \ No newline at end of file diff --git a/Source/GnssTracker_Demo/MeadowApp.cs b/Source/GnssTracker_Demo/MeadowApp.cs index 9b70b68..1ca2b36 100644 --- a/Source/GnssTracker_Demo/MeadowApp.cs +++ b/Source/GnssTracker_Demo/MeadowApp.cs @@ -147,7 +147,7 @@ private void SolarVoltageUpdated(object sender, IChangeResult e) private void GnssRmcReceived(object sender, GnssPositionInfo e) { - if (e.Valid) + if (e.IsValid) { ReportGNSSPosition(e); lastGNSSPosition = e; @@ -156,7 +156,7 @@ private void GnssRmcReceived(object sender, GnssPositionInfo e) private void GnssGllReceived(object sender, GnssPositionInfo e) { - if (e.Valid) + if (e.IsValid) { ReportGNSSPosition(e); lastGNSSPosition = e; @@ -165,7 +165,7 @@ private void GnssGllReceived(object sender, GnssPositionInfo e) private void ReportGNSSPosition(GnssPositionInfo e) { - if (e.Valid) + if (e.IsValid) { if (DateTime.UtcNow - lastGNSSPositionReportTime >= GNSSPositionReportInterval) {