【问题标题】:Getting precise and accurate location updates indoors on the Apple Watch using Core Location?使用 Core Location 在 Apple Watch 上获取室内精确和准确的位置更新?
【发布时间】:2018-12-28 23:49:03
【问题描述】:

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

    }

【问题讨论】:

    标签: ios swift core-location cllocationmanager apple-watch


    【解决方案1】:

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

    kCLLocationAccuracyBestForNavigation
    

    【讨论】:

    • 但是导航不是指驾驶而不是跑步等吗?
    • 谢谢,我会测试一下,看看我是否能得到更好的结果。我没有使用 kCLLocationAccuracyBest 的另一个原因是文档说仅在设备连接到电源时使用,这对于锻炼用例显然不实用。
    • @GarySabo 你是对的。无论如何,你可以尝试一下,你会看到的。或者,您可以在不使用 kCL... 变量的情况下设置自己的准确度
    • 我测试过了,kCLLocationAcurracyBest 和 kCCLocationAccuracyBestForNagivation 在户外都可以很好地工作,但在室内都不能很好地工作?我了解屋顶下 GPS 的局限性,但我使用过仅使用核心位置且在室内准确的其他应用,他们是如何做到的?
    • @GarySabo 哦,那是另一个问题。你能给我举一个在室内运行良好的应用程序示例吗?也许我们可以弄清楚为什么会这样。
    猜你喜欢
    • 2014-11-05
    • 1970-01-01
    • 1970-01-01
    • 2016-04-11
    • 2018-06-02
    • 1970-01-01
    • 2015-04-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多