From 85d2a39fe24b8b469fccd7d73b8aa5198922243d Mon Sep 17 00:00:00 2001 From: Justin Oros Date: Mon, 5 May 2025 18:25:05 -0700 Subject: [PATCH] Speed accuracy enhancements. Improved Speed and Trip accuracy. --- Speedo/LocationManager.swift | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/Speedo/LocationManager.swift b/Speedo/LocationManager.swift index d23ae5e..c5df7e6 100644 --- a/Speedo/LocationManager.swift +++ b/Speedo/LocationManager.swift @@ -117,20 +117,28 @@ class LocationManager: NSObject, ObservableObject, CLLocationManagerDelegate { return } + // Skip updates if accuracy is too poor (≥ 20 meters) + guard latest.horizontalAccuracy >= 0 && latest.horizontalAccuracy < 20 else { + print("Skipping update due to poor accuracy: \(latest.horizontalAccuracy)") + return + } + // Logging raw location data for debugging print("New location: \(latest.coordinate.latitude), \(latest.coordinate.longitude)") print("Speed (m/s): \(latest.speed)") - print("Speed (MPH): \(latest.speed * 2.23694)") print("Accuracy: \(latest.horizontalAccuracy)") DispatchQueue.main.async { - // Ensure speed is non-negative (as negative = invalid) + // Ensure speed is non-negative let rawSpeed = max(latest.speed, 0) - // Convert from meters per second to MPH - let mph = rawSpeed * 2.23694 + // Ignore very small fluctuations when stopped (less than 0.5 m/s) + let filteredSpeed = rawSpeed < 0.5 ? 0 : rawSpeed - // Update published properties to refresh UI + // Convert from m/s to MPH + let mph = filteredSpeed * 2.23694 + + // Update UI-bound values self.speed = Int(mph.rounded()) self.location = latest self.accuracy = latest.horizontalAccuracy