简体   繁体   English

Apple Watch 套件 - 获取标题

[英]Apple Watch Kit - get heading

Is there any information whether one can receive the 'heading' of the Apple Watch?是否有任何信息是否可以接收Apple Watch的'heading'?

I would like to display an arrow where the user needs to go, relative to the way he is currently holding the watch.我想在用户需要去的地方显示一个箭头,相对于他当前拿着手表的方式。 On the iPhone you can do this using the CLLocationManager-heading, but I'm not sure whether/how this will work on Apple Watch (I don't need the iPhone heading ;) )在 iPhone 上,您可以使用 CLLocationManager 标题来执行此操作,但我不确定这是否/如何在 Apple Watch 上工作(我不需要 iPhone 标题;))

Is there any documentation on this that you can link me to?是否有任何关于此的文档可以链接到我?

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

Since WatchKit doesn't provide access to Apple Watch's hardware (and there's no magnetometer in the watch, anyway), you'd be dealing with the heading of the phone.由于 WatchKit 不提供对 Apple Watch 硬件的访问(并且手表中没有磁力计,无论如何),您将处理手机的方向。

As such, getting getting an accurate heading while standing still isn't currently feasible.因此,在静止不动的情况下获得准确的航向目前是不可行的。 However, if you're okay with requiring some movement first, you can calculate a bearing from several recorded location coordinates.但是,如果您同意先进行一些移动,则可以根据多个记录的位置坐标计算方位。 Answers to this question (iOS) or this one (general math) might prove useful to you.答案这个问题(IOS)或这一个(一般数学)可能被证明对你有用。

You can now by using "magneticHeading" or "trueHeading" from Core Location (Apple Watch Serie 5+, watchOS 6.0+)您现在可以使用 Core Location 中的“magneticHeading”或“trueHeading”(Apple Watch Serie 5+,watchOS 6.0+)

Here is a basic example:这是一个基本示例:

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
    }
}

Then when you need the heading values you do:然后,当您需要航向值时,您可以:

locationManager.startUpdatingHeading()

And when you don't need them anymore you do:当您不再需要它们时,您可以:

locationManager.stopUpdatingHeading()

Here are all the official Apple documentations:以下是所有 Apple 官方文档:

Core Location核心位置

CLHeading CL标题

magneticHeading 磁航向

trueHeading 真实标题

Heading and Course Information basic knowledge 标题和课程信息基础知识

Don't forget to check if "heading" is available first: headingAvailable()不要忘记先检查“标题”是否可用: headingAvailable()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM