Speed accuracy enhancements.
Improved Speed and Trip accuracy.
This commit is contained in:
@@ -117,20 +117,28 @@ class LocationManager: NSObject, ObservableObject, CLLocationManagerDelegate {
|
|||||||
return
|
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
|
// Logging raw location data for debugging
|
||||||
print("New location: \(latest.coordinate.latitude), \(latest.coordinate.longitude)")
|
print("New location: \(latest.coordinate.latitude), \(latest.coordinate.longitude)")
|
||||||
print("Speed (m/s): \(latest.speed)")
|
print("Speed (m/s): \(latest.speed)")
|
||||||
print("Speed (MPH): \(latest.speed * 2.23694)")
|
|
||||||
print("Accuracy: \(latest.horizontalAccuracy)")
|
print("Accuracy: \(latest.horizontalAccuracy)")
|
||||||
|
|
||||||
DispatchQueue.main.async {
|
DispatchQueue.main.async {
|
||||||
// Ensure speed is non-negative (as negative = invalid)
|
// Ensure speed is non-negative
|
||||||
let rawSpeed = max(latest.speed, 0)
|
let rawSpeed = max(latest.speed, 0)
|
||||||
|
|
||||||
// Convert from meters per second to MPH
|
// Ignore very small fluctuations when stopped (less than 0.5 m/s)
|
||||||
let mph = rawSpeed * 2.23694
|
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.speed = Int(mph.rounded())
|
||||||
self.location = latest
|
self.location = latest
|
||||||
self.accuracy = latest.horizontalAccuracy
|
self.accuracy = latest.horizontalAccuracy
|
||||||
|
|||||||
Reference in New Issue
Block a user