2021-08-20 07:56:05 -07:00
|
|
|
import CoreLocation
|
|
|
|
|
|
|
|
|
|
class LocationHelper: NSObject, ObservableObject {
|
2021-11-29 15:59:06 -08:00
|
|
|
|
2021-08-20 07:56:05 -07:00
|
|
|
static let shared = LocationHelper()
|
2021-11-29 15:59:06 -08:00
|
|
|
|
2022-02-24 08:28:08 -10:00
|
|
|
// Apple Park
|
2022-02-24 07:57:08 -10:00
|
|
|
static let DefaultLocation = CLLocationCoordinate2D(latitude: 37.3346, longitude: -122.0090)
|
2022-02-17 18:07:41 -08:00
|
|
|
|
|
|
|
|
static let DefaultAltitude = CLLocationDistance(integerLiteral: 0)
|
2021-11-29 15:59:06 -08:00
|
|
|
|
2021-08-20 07:56:05 -07:00
|
|
|
static var currentLocation: CLLocationCoordinate2D {
|
2021-11-29 15:59:06 -08:00
|
|
|
|
2021-08-20 07:56:05 -07:00
|
|
|
guard let location = shared.locationManager.location else {
|
|
|
|
|
return DefaultLocation
|
|
|
|
|
}
|
|
|
|
|
return location.coordinate
|
|
|
|
|
}
|
2021-11-29 15:59:06 -08:00
|
|
|
|
2022-02-17 18:07:41 -08:00
|
|
|
static var currentAltitude: CLLocationDistance {
|
|
|
|
|
|
|
|
|
|
guard let altitude = shared.locationManager.location?.altitude else {
|
|
|
|
|
return DefaultAltitude
|
|
|
|
|
}
|
|
|
|
|
return altitude
|
|
|
|
|
}
|
2022-05-24 07:19:40 -07:00
|
|
|
|
|
|
|
|
static var currentTimestamp: Date {
|
|
|
|
|
|
|
|
|
|
guard let timestamp = shared.locationManager.location?.timestamp else {
|
|
|
|
|
return Date.now
|
|
|
|
|
}
|
|
|
|
|
return timestamp
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2022-02-17 18:07:41 -08:00
|
|
|
|
2021-08-20 07:56:05 -07:00
|
|
|
private let locationManager = CLLocationManager()
|
2021-11-29 15:59:06 -08:00
|
|
|
|
2021-08-20 07:56:05 -07:00
|
|
|
private override init() {
|
2021-11-29 15:59:06 -08:00
|
|
|
|
2021-08-20 07:56:05 -07:00
|
|
|
super.init()
|
|
|
|
|
locationManager.delegate = self
|
|
|
|
|
locationManager.desiredAccuracy = kCLLocationAccuracyBest
|
|
|
|
|
locationManager.requestWhenInUseAuthorization()
|
|
|
|
|
locationManager.startUpdatingLocation()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
extension LocationHelper: CLLocationManagerDelegate {
|
|
|
|
|
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { }
|
2021-11-29 15:59:06 -08:00
|
|
|
|
2021-08-20 07:56:05 -07:00
|
|
|
public func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
|
|
|
|
|
print("Location manager failed with error: \(error.localizedDescription)")
|
|
|
|
|
}
|
2021-11-29 15:59:06 -08:00
|
|
|
|
2021-08-20 07:56:05 -07:00
|
|
|
public func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
|
|
|
|
|
print("Location manager changed the status: \(status)")
|
|
|
|
|
}
|
|
|
|
|
}
|