Swift

来源:互联网 发布:医学文献翻译软件 编辑:程序博客网 时间:2024/05/06 10:56
CoreLocation这个定位框架除了可以获取设备的位置数据,还可以获取设备的方向(可以用来实现指南针功能等)。

1,CLHeading对象通过一组属性提供航向相关数据:
magneticHeading :磁极方向(磁北对应于随时间变化的地球磁场极点)
trueHeading :真实方向(真北始终指向地理北极点)
headingAccuracy :方向的精度
timestamp :Core Loaction确定位置时的时间戳
description : 方向数据

2,方向值的含义
不管是磁极方向还是真实方向,值的单位是度,类型为CLLocationDirection,即双精度浮点数。
0.0 :前进方向为北
90.0 :前进方向为东
180.0 :前进方向为南
270.0 :前进方向为西

3,下面通过样例进行演示
原文:Swift - 使用CoreLocation获取设备方向(真实方向,磁极方向)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import UIKit
import CoreLocation
 
class ViewControllerUIViewControllerCLLocationManagerDelegate {
     
    //定位管理器
    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)"
    }  
}

原文出自:www.hangge.com  转载请保留原文链接:http://www.hangge.com/blog/cache/detail_784.html
原创粉丝点击