返回 导航

Swift

hangge.com

Swift - 异步编程库PromiseKit使用详解10(CLLocationManager的扩展)

作者:hangge | 2018-12-24 08:10
    CoreLocation iOS 中一个提供设备定位的框架。我们可以使用 CoreLocation 来获取到当前的位置数据,比如经度、纬度、海拔信息等。
    PromiseKit 同样也对其进行了扩展,方便我们使用。我们不再需要去实现相关的代理方法,只需通过链式调用的方式即可获取到结果。同时我们也不需写申请定位权限相关代码,这些操作它内部也已经帮我们处理好了。

十一、CLLocationManager 的扩展

1,安装配置

(1)首先要安装 PromiseKit 库 ,具体步骤可以参考我之前的文章:

(2)接着安装 PromiseKit CoreLocation 扩展库,从 GitHub 上下载最新的代码:

(3)将下载下来的源码包中 PMKCoreLocation.xcodeproj 拖拽至你的工程中

(4)工程 -> General -> Embedded Binaries 项,把 PMKCoreLocation.framework 添加进来。

(5)最后,在需要使用扩展的地方将其 import 进来就可以了
import PMKCoreLocation

2,准备工作

由于苹果的安全策略,使用定位功能前我们还需要在 info.plist 里加入定位描述:
  • Privacy - Location When In Use Usage Description:我们需要通过您的地理位置信息获取您周边的相关数据 
  • Privacy - Location Always Usage Description:我们需要通过您的地理位置信息获取您周边的相关数据 
  • Privacy - Location Always and When In Use Usage Description:我们需要通过您的地理位置信息获取您周边的相关数据

3,使用样例

(1)下面代码在程序启动后,自动获取当前设备相关的位置数据(经度,纬度,海拔,速度等信息)并显示在界面上:
import UIKit
import PromiseKit
import PMKCoreLocation

class ViewController: UIViewController {
    
    @IBOutlet weak var label1: UILabel!
    @IBOutlet weak var label2: UILabel!
    @IBOutlet weak var label3: UILabel!
    @IBOutlet weak var label4: UILabel!
    @IBOutlet weak var label5: UILabel!
    @IBOutlet weak var label6: UILabel!
    @IBOutlet weak var label7: UILabel!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        //获取当前的定位信息
        _ = CLLocationManager.requestLocation().done { locations in
            //获取最新的坐标
            let currLocation:CLLocation = locations.last!
            self.label1.text = "经度:\(currLocation.coordinate.longitude)"
            //获取纬度
            self.label2.text = "纬度:\(currLocation.coordinate.latitude)"
            //获取海拔
            self.label3.text = "海拔:\(currLocation.altitude)"
            //获取水平精度
            self.label4.text = "水平精度:\(currLocation.horizontalAccuracy)"
            //获取垂直精度
            self.label5.text = "垂直精度:\(currLocation.verticalAccuracy)"
            //获取方向
            self.label6.text = "方向:\(currLocation.course)"
            //获取速度
            self.label7.text = "速度:\(currLocation.speed)"
        }
    }
}

(2)下面样例不断地获取定位信息,只有当精度小于 50 时,才返回结果(done 方法才会执行)。
requestLocation 这个扩展方法还有个 satisfying 参数,它是一个函数闭包(block),我们可以在这里对当前定位信息(CLLocation)进行判断过滤,只有满足条件的才返回。
_ = CLLocationManager
    .requestLocation(authorizationType: .automatic, satisfying: { location -> Bool in
        //只有等到水平精度小于50的时候才返回结果
        return location.horizontalAccuracy < 50 ? true : false
    }).done { locations in
        //获取最新的坐标
        let currLocation:CLLocation = locations.last!
        self.label1.text = "经度:\(currLocation.coordinate.longitude)"
        //获取纬度
        self.label2.text = "纬度:\(currLocation.coordinate.latitude)"
        //获取海拔
        self.label3.text = "海拔:\(currLocation.altitude)"
        //获取水平精度
        self.label4.text = "水平精度:\(currLocation.horizontalAccuracy)"
        //获取垂直精度
        self.label5.text = "垂直精度:\(currLocation.verticalAccuracy)"
        //获取方向
        self.label6.text = "方向:\(currLocation.course)"
        //获取速度
        self.label7.text = "速度:\(currLocation.speed)"
    }
评论

全部评论(0)

回到顶部