Meshtastic-Apple/Meshtastic/Helpers/LocationHelper.swift

95 lines
3.4 KiB
Swift
Raw Normal View History

import Foundation
2021-08-20 07:56:05 -07:00
import CoreLocation
2023-09-19 17:06:47 -07:00
import MapKit
2021-08-20 07:56:05 -07:00
class LocationHelper: NSObject, ObservableObject, CLLocationManagerDelegate {
2023-04-18 00:09:13 -07:00
static let shared = LocationHelper()
var locationManager = CLLocationManager()
2023-09-19 17:06:47 -07:00
//@Published var region = MKCoordinateRegion()
@Published var authorizationStatus: CLAuthorizationStatus?
override init() {
super.init()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
locationManager.pausesLocationUpdatesAutomatically = true
locationManager.allowsBackgroundLocationUpdates = true
locationManager.activityType = .other
}
2023-04-18 00:09:13 -07:00
// Apple Park
static let DefaultLocation = CLLocationCoordinate2D(latitude: 37.3346, longitude: -122.0090)
static var currentLocation: CLLocationCoordinate2D {
guard let location = shared.locationManager.location else {
return DefaultLocation
}
return location.coordinate
2023-04-08 00:34:39 -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
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 {
sats = 9
2023-03-06 10:33:18 -08:00
} else if 31...45 ~= shared.locationManager.location?.horizontalAccuracy ?? 0 {
sats = 7
2023-03-06 10:33:18 -08:00
} else if 46...60 ~= shared.locationManager.location?.horizontalAccuracy ?? 0 {
sats = 5
}
2023-04-18 00:09:13 -07:00
} else if shared.locationManager.location?.verticalAccuracy ?? 0 < 0 && 60...300 ~= shared.locationManager.location?.horizontalAccuracy ?? 0 {
sats = 3
2023-04-18 00:09:13 -07:00
} else if shared.locationManager.location?.verticalAccuracy ?? 0 < 0 && shared.locationManager.location?.horizontalAccuracy ?? 0 > 300 {
sats = 2
}
return sats
}
func locationManagerDidChangeAuthorization(_ manager: CLLocationManager) {
switch manager.authorizationStatus {
case .authorizedAlways:
authorizationStatus = .authorizedAlways
case .authorizedWhenInUse:
authorizationStatus = .authorizedWhenInUse
locationManager.requestLocation()
case .restricted:
authorizationStatus = .restricted
case .denied:
authorizationStatus = .denied
case .notDetermined:
authorizationStatus = .notDetermined
locationManager.requestAlwaysAuthorization()
default:
break
}
2023-04-08 00:34:39 -07:00
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let chimeOnLocationUpdate = true
// locationManager.stopUpdatingLocation()
// locations.last.map {
// region = MKCoordinateRegion(
// center: $0.coordinate,
// span: .init(latitudeDelta: 0.01, longitudeDelta: 0.01)
// )
// }
// Play a sound so it's easy to tell when a location update occurs while the app is in the background.
if chimeOnLocationUpdate && !locations.isEmpty {
// setSessionActiveWithMixing(true) // Ducks the audio of other apps when playing the chime.
// playSound()
}
// Always process all of the provided locations. Don't assume the array only contains a single location.
for location in locations {
print("process a location")
// displayNewBreadcrumbOnMap(location)
}
2023-04-08 00:34:39 -07:00
}
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
print("Location manager error: \(error.localizedDescription)")
}
2021-08-20 07:56:05 -07:00
}