import UIKit
import CoreLocation
class 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)"
}
}
|