代码之家  ›  专栏  ›  技术社区  ›  GarySabo

使用核心位置在苹果手表上获得精确准确的位置更新?

  •  0
  • GarySabo  · 技术社区  · 7 年前

    我正在尝试获取用于训练跟踪的位置更新(室内),因此我需要非常精确和持续的位置更新,但在测试中,代理回调似乎不太准确。例如,移动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
    
        }
    
    1 回复  |  直到 7 年前
        1
  •  0
  •   Robert Dresler Gustavo Vollbrecht    7 年前

    你准备好了吗 kCLLocationAccuracyBest 作为 desiredAccuracy . 存在变量 CLLocationAccuracy

    kCLLocationAccuracyBestForNavigation