测试已测试 | ✓ |
语言语言 | SwiftSwift |
许可 | MIT |
发布上次发布 | 2017年2月 |
SwiftSwift 版本 | 3.0 |
SPM支持 SPM | ✓ |
由 Juan Cruz Ghigliani 维护。
这个库是为了简化 CLLocationManager 的使用而创建的。LocationRequestManager 是 CLLocationManager 的一个包装器,用于处理一组位置请求。对于每个请求,您可以指定一些参数,如超时、距离过滤器、精度。
要运行示例项目,请克隆仓库,然后首先从 Example 目录运行 pod install
。
您应该创建一个 LocationRequestManager 实例,这个类将负责初始化 locationManager,向用户请求授权,并处理所有用户的位置请求。
当您需要当前位置时,您应该创建一个新的 LocationRequest,并将此请求传递给 locationManager。位置管理器将监听您的位置,直到满足您的请求。
您可以发送多个同时请求,位置管理器将活跃,直到满足所有请求。
let locationRequestManager: LocationRequestManager = LocationRequestManager()
// we create to request
var basicRequest:LocationRequest?
var timeoutRequest:LocationRequest?
self.basicRequest = LocationRequest{(currentLocation:CLLocation?,error: NSError?)->Void in
print("\(self.basicRequest!.status) - \(currentLocation) - \(error)")
}
self.timeoutRequest = LocationRequest{(currentLocation:CLLocation?,error: NSError?)->Void in
print("\(self.timeoutRequest!.status) - \(currentLocation) - \(error)")
}
self.timeoutRequest!.desiredAccuracy = kCLLocationAccuracyBest // CLLocationAccuracy
self.timeoutRequest!.timeout = 10
// performRequest() add and launch the request. We can perform more than one request in parallel . Location manager will be active until satisfy this two request o reach the timeout limit
locationRequestManager.performRequest(self.basicRequest!)
locationRequestManager.performRequest(self.timeoutRequest!)
向管理器添加请求的另一种方法
// We add this 2 request to the manager, and then we decide qhen to perform the requests. Location manager will be active until satisfy this two request o reach the timeout limit
locationRequestManager.addRequest(self.basicRequest!)
locationRequestManager.addRequest(self.timeoutRequest!)
...
locationRequestManager.performRequests()
当您调用 performRequest() 时,库将检查权限状态,并在必要时根据您的 plist 设置调用 requestAlwaysAuthorization 或 requestWhenInUseAuthorization(kCLAuthorizationStatusAuthorizedAlways 或 kCLAuthorizationStatusAuthorizedWhenInUse)
但您也可以在需要时调用权限请求流程,例如
let locationRequestManager: LocationRequestManager = LocationRequestManager()
locationRequestManager.requestAlwaysAuthorization { (status:CLAuthorizationStatus) in
print("Auth Status: \(status)")
}
或
let locationRequestManager: LocationRequestManager = LocationRequestManager()
locationRequestManager.requestWhenInUseAuthorization { (status:CLAuthorizationStatus) in
print("Auth Status: \(status)")
}
创建位置请求后,您可以指定一组参数,这些参数将用于检测位置管理器何时可以满足您的需求
位置数据的精度。您应该为这个属性分配一个值,使其适合您的使用场景。例如,如果您只需要在千米范围内的当前位置,您应该指定 kCLLocationAccuracyKilometer,而不是 kCLLocationAccuracyBestForNavigation。使用更高的精度需要更多的时间和能量。
public var desiredAccuracy:CLLocationAccuracy = kCLLocationAccuracyThreeKilometers
设备必须在水平方向上移动的最小距离(以米为单位),在更新事件生成之前。
public var distanceFilter:CLLocationDistance = kCLDistanceFilterNone;
如果为 true,位置管理器不会停止返回位置,这对于导航应用很有用。
public var recurrent:Bool = false
有效返回位置的时间限制。超时后将会调用回调块,状态为.Timeout
public var timeout:NSTimeInterval?
在位置跟踪结束后将被调用的回调块
public var block:CompleteRequestBlock?
locationManager返回的最新位置值
public var latestLocation:CLLocation?
locationManager返回的最新错误值
public var latestError:NSError?
LocationRequestManager可通过CocoaPods获取。要安装,只需将以下行添加到您的Podfile中
pod "LocationRequestManager"
Juan Cruz Ghigliani,[email protected]
LocationRequestManager在MIT许可下可用。更多信息请参阅LICENSE文件。