Я пытаюсь получать обновления местоположения для отслеживания тренировок (в помещении), поэтому мне нужны очень точные и постоянные обновления местоположения, но при тестировании обратные вызовы делегатов не кажутся очень точными.Например, перемещение на 20-30 футов не вызывает обновления местоположения большую часть времени.Есть ли что-то в моем коде ниже, что может вызвать эту неточность?
import CoreLocation
protocol UserLocationDelegate: class {
func didUpdateUserLocation(_ manager: WorkoutLocationManager, distance: CLLocationDistance)
}
class WorkoutLocationManager: NSObject, CLLocationManagerDelegate {
deinit {
self.locationManager?.stopUpdatingLocation()
}
private var locationManager: CLLocationManager?
var previousLocation: CLLocation?
weak var userLocationDelgate: UserLocationDelegate?
public func getUserLocation() {
guard CLLocationManager.locationServicesEnabled() else {
print("User does not have location services enabled")
return
}
locationManager = CLLocationManager()
locationManager?.delegate = self
locationManager?.allowsBackgroundLocationUpdates = true
locationManager?.desiredAccuracy = kCLLocationAccuracyBest
locationManager?.activityType = .fitness //test as the docs say this will turn OFF indoor tracking
let locationAuthorizationStatus = CLLocationManager.authorizationStatus()
switch locationAuthorizationStatus {
case .authorizedAlways:
print("location authorized Always")
locationManager?.startUpdatingLocation()
case .authorizedWhenInUse:
print("location authorized When in Use")
locationManager?.startUpdatingLocation()
case .denied:
print("location authorization denied")
locationManager?.requestAlwaysAuthorization()
case .notDetermined:
print("location authorization not determined")
locationManager?.requestAlwaysAuthorization()
case .restricted:
print("location authorization restricted")
locationManager?.requestAlwaysAuthorization()
}
}
// MARK: - CLLocationManagerDelegate
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
print("did update locations called")
if previousLocation == nil {
previousLocation = locations.first
} else {
guard let latest = locations.first else { return }
let distanceInMeters = previousLocation?.distance(from: latest) ?? 0
if distanceInMeters > 0 {
let distanceInFeet = distanceInMeters * 3.28
print("distance in feet: \(distanceInFeet)")
userLocationDelgate?.didUpdateUserLocation(self, distance: distanceInFeet
)
}
previousLocation = latest
}
}
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
print("location manager error = \(error)")
}
}
import WatchKit
import Foundation
import CoreLocation
class InterfaceController: WKInterfaceController, UserLocationDelegate {
func didUpdateUserLocation(_ manager: WorkoutLocationManager, distance: CLLocationDistance) {
locationLabel.setText("\(distance.rounded().description) feet")
}
let workoutLocationManager = WorkoutLocationManager()
@IBOutlet weak var locationLabel: WKInterfaceLabel!
override func awake(withContext context: Any?) {
super.awake(withContext: context)
workoutLocationManager.getUserLocation()
workoutLocationManager.userLocationDelgate = self
}