ios - 可以获取位置,但不能获取航向

标签 ios swift core-location

我目前只使用模拟器,但在 iOS 模拟器上 CoreLocation in swift 遇到了问题。我通过此代码获得了位置更新,但没有航向。我不想要当然,我正在尝试制作一个指南针类型的应用程序,它会显示目标的方位。

class CompassViewController: UIViewController, CLLocationManagerDelegate {

    var lm:CLLocationManager = CLLocationManager()

    override func viewDidLoad() {
        super.viewDidLoad()

        print ("view did load")

        lm.delegate = self;
        lm.desiredAccuracy = kCLLocationAccuracyBest
        lm.requestWhenInUseAuthorization()
        lm.startUpdatingLocation()
        lm.startUpdatingHeading()

    }

    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        var locValue:CLLocationCoordinate2D = manager.location!.coordinate
        var course = manager.location!.course
               print("locations = \(locations)")
    }

    func headingManger(_ manager: CLLocationManager, didUpdateHeading: [CLHeading]) {
        var heading = manager.heading?.magneticHeading
        print("heading = \(heading)")
    }

}

我已经尝试了几种组合。我尝试做一个处理位置和航向的函数,但根本没有打印任何数据。我也试过只使用 headingManager 函数,但没有打印任何内容。上面的当前代码将打印位置,而不是标题。

我怎样才能去上类?

最佳答案

Compass 不在模拟器支持的硬件交互列表中,CLLocationManager headingAvailable 报告它在模拟器上不可用。

此外,在本文档中:

http://developer.apple.com/library/ios/#documentation/CoreLocation/Reference/CLLocationManager_Class/CLLocationManager/CLLocationManager.html

一些位置服务需要在给定的设备上存在特定的硬件。例如,航向信息仅适用于包含硬件罗盘的设备。此类定义了几种方法,您可以使用这些方法来确定当前可用的服务。 具体来说,CLLocationManager 有这个类属性来检查指南针是否可用:

  • (BOOL)headingAvailable 如果我在模拟器下运行它:

print("headingAvailable: (Int(CLLocationManager.headingAvailable()))") 输出:

2011-11-08 22:38:26.873 Craplet[39975:b603] headingAvailable: 0

关于ios - 可以获取位置,但不能获取航向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44479865/

相关文章:

ios - 通过 UiControls 过滤 CoreData 记录的最佳实践

ios - 是否可以控制 "back to"按钮?

ios - 来自 UICollectionViewCell 中的 Class 对象的 UITableView 数据源

ios - 无法通过下标赋值 : subscript is get-only

iphone - 位置追踪

ios - 应用圆形区域的地理围栏时应用程序崩溃

ios - 将对象添加到NSMutableArray时无法识别的选择器

ios - swift.h 找不到 Swift 类

ios - 如何指示用户滑动的方向,而不是 Swift 中的下一个和上一个 View Controller ?

iphone - 将位置存储为字符串,从字符串中获取位置以显示在 map 上。 iOS