我正在尝试获取位置更新以进行锻炼跟踪(室内),因此我需要非常精确且恒定的位置更新,但是在测试委托回调时似乎不太准确。例如,移动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

    }

最佳答案

您将kCLLocationAccuracyBest设置为desiredAccuracyCLLocationAccuracy有一个变量,应该更准确

kCLLocationAccuracyBestForNavigation

09-09 18:41