In addition to acquiring the location data of the device, the corelocation can also obtain the orientation of the device (which can be used to implement the compass function, etc.).
The 1,clheading object provides heading-related data through a set of properties:magneticheading: Polarity direction (magnetic North corresponds to the pole of Earth's magnetic field over time)trueheading: True direction (True North always points to geographic North Point)headingaccuracy: precision of directionTimestamp:core loaction time stamp when determining positionDescription: Direction data
2, meaning of the direction valueWhether it is the direction of the magnetic pole or the true direction, the unit of value is the degree, the type is cllocationdirection, that is, double precision floating point number.0.0: Forward direction for North90.0: Forward direction for East180.0: Forward direction for the South270.0: Forward direction for West
3, the following sample to demonstrate
| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647 |
import UIKitimport CoreLocationclass ViewController: UIViewController, CLLocationManagerDelegate { //定位管理器 let locationManager:CLLocationManager = CLLocationManager() @IBOutlet weak var label1: UILabel! @IBOutlet weak var label2: UILabel! @IBOutlet weak var label3: UILabel! @IBOutlet weak var label4: UILabel! override func viewDidLoad() { super.viewDidLoad() //设置定位服务管理器代理 locationManager.delegate = self //设置定位进度 locationManager.desiredAccuracy = kCLLocationAccuracyBest //发送授权申请 locationManager.requestAlwaysAuthorization() } //获取设备是否允许使用定位服务 func locationManager(manager: CLLocationManager!, didChangeAuthorizationStatus status: CLAuthorizationStatus) { if status == CLAuthorizationStatus.NotDetermined || status == CLAuthorizationStatus.Denied{ }else{ //允许使用定位服务的话,开启定位服务更新 locationManager.startUpdatingHeading() println("方向定位开始") //关闭定位 //locationManager.stopUpdatingHeading() } } //方向改变执行 func locationManager(manager: CLLocationManager!, didUpdateHeading newHeading: CLHeading!) { label1.text = "磁极方向:\(newHeading.magneticHeading)" label2.text = "真实方向:\(newHeading.trueHeading)" label3.text = "方向的精度:\(newHeading.headingAccuracy)" label4.text = "时间:\(newHeading.timestamp)" } } |
Swift-use corelocation to get device orientation (true direction, pole direction)