【问题标题】:Apple Watch Kit - get headingApple Watch Kit - 获取方向
【发布时间】:2015-06-02 10:37:57
【问题描述】:

是否有任何信息是否可以接收 Apple Watch 的“标题”?

我想在用户需要去的地方显示一个箭头,相对于他当前持有手表的方式。在 iPhone 上,您可以使用 CLLocationManager 标题执行此操作,但我不确定这是否/如何在 Apple Watch 上工作(我不需要 iPhone 标题;))

有没有这方面的文档可以链接到我?

【问题讨论】:

标签: ios apple-watch


【解决方案1】:

您现在可以使用 Core Location 中的“magneticHeading”或“trueHeading”(Apple Watch Serie 5+、watchOS 6.0+)

这是一个基本的例子:

import CoreLocation

class yourClass {
    let locationManager = CLLocationManager()

    init() {
        setupCoreLocation()
    }

    func setupCoreLocation() {
        guard CLLocationManager.headingAvailable() else { return }
        locationManager.delegate = self
        locationManager.requestWhenInUseAuthorization()
        // additional setup available if needed
    }
}

extension yourClass: CLLocationManagerDelegate {
    func locationManager(_ manager: CLLocationManager, didUpdateHeading newHeading: CLHeading) {
        let magneticHeading = newHeading.magneticHeading
        let trueHeading = newHeading.trueHeading
        // Do something with the new heading values
    }
}

然后,当您需要标题值时:

locationManager.startUpdatingHeading()

当你不再需要它们时,你会这样做:

locationManager.stopUpdatingHeading()

以下是所有官方 Apple 文档:

Core Location

CLHeading

magneticHeading

trueHeading

Heading and Course Information basic knowledge

别忘了先检查“标题”是否可用:headingAvailable()

【讨论】:

    【解决方案2】:

    由于 WatchKit 不提供对 Apple Watch 硬件的访问(而且手表中也没有磁力计),因此您需要处理手机的航向。

    因此,目前无法在静止不动的情况下获得准确的航向。但是,如果您同意先进行一些移动,则可以从多个记录的位置坐标计算方位。 this question(iOS)或this one(一般数学)的答案可能对您有用。

    【讨论】:

      【解决方案3】:

      Apple watch 中没有指南针...您可以根据连续的 GPS 位置猜测航向(GPS 也在 iphone 上运行...)依靠 iPhone 的指南针可能不合适!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-02-26
        • 2020-07-19
        • 1970-01-01
        • 1970-01-01
        • 2015-10-06
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多