热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

适用于iOS的磁性标题示例代码-MagneticHeadingsamplecodeforiOS

CananyoneprovidemewithashortsnippetthatwillreturnmethemagneticheadingoftheiPhone?I

Can anyone provide me with a short snippet that will return me the magnetic heading of the iPhone? I do not want Objective-C please. I need it in Swift.

任何人都可以提供一个简短的片段,让我回到iPhone的磁性标题?我不想要Objective-C。我在Swift中需要它。

I have written these lines so far but it does not return me any value:

到目前为止我已写过这些行,但它并没有给我任何价值:

let locManager = CLLocationManager()
locManager.desiredAccuracy = kCLLocationAccuracyBest
locManager.requestWhenInUseAuthorization()
locManager.startUpdatingLocation()

locManager.startUpdatingHeading()
locManager.headingOrientation = .portrait
locManager.headingFilter = kCLHeadingFilterNone

print(locManager.heading?.trueHeading.binade as Any)

Thanks!

谢谢!

1 个解决方案

#1


2  

You didn't set the delegate for the location manager. iOS does not update your location right away. Rather, it will call a function provided by your delegate when it has a location / heading update. The reason behind this setup is efficiency. Instead of 10 apps having 10 different location managers competing for time on the GPS hardware, these 10 location managers will request to be notified when the GPS has an update.

您没有为位置管理器设置委托。 iOS不会立即更新您的位置。相反,当它具有位置/标题更新时,它将调用您的代理提供的功能。这种设置背后的原因是效率。这些10个位置管理员将在GPS更新时请求通知,而不是10个不同的位置管理器在GPS硬件上竞争时间的10个应用程序。

Try this:

尝试这个:

class ViewController: UIViewController, CLLocationManagerDelegate {
    @IBOutlet weak var label: UILabel!
    var locManager = CLLocationManager()

    override func viewDidLoad() {
        super.viewDidLoad()

        locManager.desiredAccuracy = kCLLocationAccuracyBest
        locManager.requestWhenInUseAuthorization()
        locManager.headingOrientation = .portrait
        locManager.headingFilter = kCLHeadingFilterNone
        locManager.delegate = self // you forgot to set the delegate

        locManager.startUpdatingLocation()
        locManager.startUpdatingHeading()
    }

    // MARK: -
    // MARK: CLLocationManagerDelegate
    func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
        print("Location Manager failed: \(error)")
    }

    // Heading readings tend to be widely inaccurate until the system has calibrated itself
    // Return true here allows iOS to show a calibration view when iOS wants to improve itself
    func locationManagerShouldDisplayHeadingCalibration(_ manager: CLLocationManager) -> Bool {
        return true
    }

    // This function will be called whenever your heading is updated. Since you asked for best
    // accuracy, this function will be called a lot of times. Better make it very efficient
    func locationManager(_ manager: CLLocationManager, didUpdateHeading newHeading: CLHeading) {
        label.text = "\(newHeading.magneticHeading)"
    }
}

推荐阅读
author-avatar
林友骏091
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有