2023-04-22 12:31:06 -07:00
|
|
|
import Foundation
|
2021-08-20 07:56:05 -07:00
|
|
|
import CoreLocation
|
|
|
|
|
|
2023-04-22 12:31:06 -07:00
|
|
|
class LocationHelper: NSObject, ObservableObject, CLLocationManagerDelegate {
|
|
|
|
|
|
2023-04-18 00:09:13 -07:00
|
|
|
static let shared = LocationHelper()
|
2023-04-22 12:31:06 -07:00
|
|
|
var locationManager = CLLocationManager()
|
|
|
|
|
|
|
|
|
|
@Published var authorizationStatus: CLAuthorizationStatus?
|
|
|
|
|
|
|
|
|
|
override init() {
|
|
|
|
|
|
|
|
|
|
super.init()
|
|
|
|
|
locationManager.delegate = self
|
|
|
|
|
locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
|
|
|
|
|
locationManager.pausesLocationUpdatesAutomatically = true
|
|
|
|
|
locationManager.allowsBackgroundLocationUpdates = true
|
|
|
|
|
locationManager.activityType = .otherNavigation
|
|
|
|
|
}
|
2023-04-08 00:34:39 -07:00
|
|
|
|
2023-04-18 00:09:13 -07:00
|
|
|
// Apple Park
|
|
|
|
|
static let DefaultLocation = CLLocationCoordinate2D(latitude: 37.3346, longitude: -122.0090)
|
|
|
|
|
static let DefaultAltitude = CLLocationDistance(integerLiteral: 0)
|
|
|
|
|
static let DefaultSpeed = CLLocationSpeed(integerLiteral: 0)
|
|
|
|
|
static let DefaultHeading = CLLocationDirection(integerLiteral: 0)
|
2023-04-22 12:31:06 -07:00
|
|
|
|
2023-04-18 00:09:13 -07:00
|
|
|
static var currentLocation: CLLocationCoordinate2D {
|
2023-04-22 12:31:06 -07:00
|
|
|
|
2023-04-18 00:09:13 -07:00
|
|
|
guard let location = shared.locationManager.location else {
|
|
|
|
|
return DefaultLocation
|
|
|
|
|
}
|
|
|
|
|
return location.coordinate
|
2023-04-08 00:34:39 -07:00
|
|
|
}
|
2023-04-22 12:31:06 -07:00
|
|
|
|
2023-04-18 00:09:13 -07:00
|
|
|
static var currentAltitude: CLLocationDistance {
|
2023-04-22 12:31:06 -07:00
|
|
|
|
2023-04-18 00:09:13 -07:00
|
|
|
guard let altitude = shared.locationManager.location?.altitude else {
|
|
|
|
|
return DefaultAltitude
|
|
|
|
|
}
|
|
|
|
|
return altitude
|
|
|
|
|
}
|
2023-04-22 12:31:06 -07:00
|
|
|
|
2023-04-18 00:09:13 -07:00
|
|
|
static var currentSpeed: CLLocationSpeed {
|
2023-04-22 12:31:06 -07:00
|
|
|
|
2023-04-18 00:09:13 -07:00
|
|
|
guard let speed = shared.locationManager.location?.speed else {
|
|
|
|
|
return DefaultSpeed
|
|
|
|
|
}
|
|
|
|
|
return speed
|
|
|
|
|
}
|
2023-04-22 12:31:06 -07:00
|
|
|
|
2023-04-18 00:09:13 -07:00
|
|
|
static var currentHeading: CLLocationDirection {
|
2023-04-22 12:31:06 -07:00
|
|
|
|
2023-04-18 00:09:13 -07:00
|
|
|
guard let heading = shared.locationManager.location?.course else {
|
|
|
|
|
return DefaultHeading
|
|
|
|
|
}
|
|
|
|
|
return heading
|
|
|
|
|
}
|
2023-04-22 12:31:06 -07:00
|
|
|
|
2023-04-18 00:09:13 -07:00
|
|
|
static var currentTimestamp: Date {
|
2023-04-22 12:31:06 -07:00
|
|
|
|
2023-04-18 00:09:13 -07:00
|
|
|
guard let timestamp = shared.locationManager.location?.timestamp else {
|
|
|
|
|
return Date.now
|
2023-04-08 00:34:39 -07:00
|
|
|
}
|
2023-04-18 00:09:13 -07:00
|
|
|
return timestamp
|
|
|
|
|
}
|
2023-04-22 12:31:06 -07:00
|
|
|
|
2023-04-18 00:09:13 -07:00
|
|
|
static var satsInView: Int {
|
2022-10-04 18:47:12 -07:00
|
|
|
// If we have a position we have a sat
|
|
|
|
|
var sats = 1
|
2022-10-04 16:48:49 -07:00
|
|
|
if shared.locationManager.location?.verticalAccuracy ?? 0 > 0 {
|
|
|
|
|
sats = 4
|
2023-03-06 10:33:18 -08:00
|
|
|
if 0...5 ~= shared.locationManager.location?.horizontalAccuracy ?? 0 {
|
2022-10-04 18:47:12 -07:00
|
|
|
sats = 12
|
2023-03-06 10:33:18 -08:00
|
|
|
} else if 6...15 ~= shared.locationManager.location?.horizontalAccuracy ?? 0 {
|
2022-10-04 18:47:12 -07:00
|
|
|
sats = 10
|
2023-03-06 10:33:18 -08:00
|
|
|
} else if 16...30 ~= shared.locationManager.location?.horizontalAccuracy ?? 0 {
|
2022-11-16 19:04:22 -08:00
|
|
|
sats = 9
|
2023-03-06 10:33:18 -08:00
|
|
|
} else if 31...45 ~= shared.locationManager.location?.horizontalAccuracy ?? 0 {
|
2022-11-16 19:04:22 -08:00
|
|
|
sats = 7
|
2023-03-06 10:33:18 -08:00
|
|
|
} else if 46...60 ~= shared.locationManager.location?.horizontalAccuracy ?? 0 {
|
2022-11-16 19:04:22 -08:00
|
|
|
sats = 5
|
2022-10-04 16:48:49 -07:00
|
|
|
}
|
2023-04-18 00:09:13 -07:00
|
|
|
} else if shared.locationManager.location?.verticalAccuracy ?? 0 < 0 && 60...300 ~= shared.locationManager.location?.horizontalAccuracy ?? 0 {
|
2022-10-04 16:48:49 -07:00
|
|
|
sats = 3
|
2023-04-18 00:09:13 -07:00
|
|
|
} else if shared.locationManager.location?.verticalAccuracy ?? 0 < 0 && shared.locationManager.location?.horizontalAccuracy ?? 0 > 300 {
|
2022-10-04 16:48:49 -07:00
|
|
|
sats = 2
|
|
|
|
|
}
|
|
|
|
|
return sats
|
|
|
|
|
}
|
2023-04-22 12:31:06 -07:00
|
|
|
|
|
|
|
|
func locationManagerDidChangeAuthorization(_ manager: CLLocationManager) {
|
|
|
|
|
switch manager.authorizationStatus {
|
|
|
|
|
case .authorizedWhenInUse:
|
|
|
|
|
authorizationStatus = .authorizedWhenInUse
|
|
|
|
|
locationManager.requestLocation()
|
|
|
|
|
break
|
|
|
|
|
case .restricted:
|
|
|
|
|
authorizationStatus = .restricted
|
|
|
|
|
break
|
|
|
|
|
case .denied:
|
|
|
|
|
authorizationStatus = .denied
|
|
|
|
|
break
|
|
|
|
|
case .notDetermined:
|
|
|
|
|
authorizationStatus = .notDetermined
|
|
|
|
|
locationManager.requestWhenInUseAuthorization()
|
|
|
|
|
break
|
|
|
|
|
default:
|
|
|
|
|
break
|
|
|
|
|
}
|
2023-04-08 00:34:39 -07:00
|
|
|
}
|
2023-04-22 12:31:06 -07:00
|
|
|
|
|
|
|
|
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
|
|
|
|
|
|
2023-04-08 00:34:39 -07:00
|
|
|
}
|
2023-04-22 18:12:48 -07:00
|
|
|
|
|
|
|
|
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
|
|
|
|
|
print("Location manager error: \(error.localizedDescription)")
|
|
|
|
|
}
|
2021-08-20 07:56:05 -07:00
|
|
|
}
|