Commit 15accfaa by Sarkizz

优化定位逻辑

parent f113e76f
......@@ -37,8 +37,6 @@ public class MRLocation: NSObject {
return _status
}
}
/// 根据状态判断是否定位的回调
public var authorization: MRlocationAuthorization?
/// 最后一次获取的定位
public var lastLocation: CLLocation?
......@@ -54,29 +52,54 @@ public class MRLocation: NSObject {
}
private var _status: LocationStatus = .idle
private var callbacks = [LocationCallback]()
private var stayCallbacks = [LocationCallback]()
public convenience init(type: LocationType = .system) {
self.init()
locationType = type
}
}
// MARK: Public
extension MRLocation {
/// 是否授权
public var isAuthorized: Bool {
let status = CLLocationManager.authorizationStatus()
return status == .authorizedWhenInUse || status == .authorizedAlways || status == .notDetermined
}
/// 定位是否开启
public var isEnable: Bool {
return CLLocationManager.locationServicesEnabled()
}
/// 开始定位
/// - Parameter finish: 定位回调
/// - Parameter stay: 是否常驻,若常驻,则这个回调在每次定位后都会调用
/// - Parameter force: 是否强制定位。强制定位会无视当前状态直接调用定位函数
public func startLocation(finish: MRLocationSystemFinish? = nil,
authorization: MRlocationAuthorization? = nil,
stay: Bool = false, force: Bool = false) {
if !isEnable {
return
}
let isStartLocating = authorization?(CLLocationManager.authorizationStatus()) ?? true
if isStartLocating {
/// 存储回调
if let finish = finish {
let c = LocationCallback(callback: finish, isStay: stay)
callbacks.append(c)
let c = LocationCallback(callback: finish)
if stay {
stayCallbacks.append(c)
} else {
callbacks.append(c)
}
}
if _status != .locating || force {
switch locationType {
case .system:
systemLocation.stopUpdatingLocation()
systemLocation.startUpdatingLocation()
_status = .locating
}
......@@ -86,6 +109,9 @@ public class MRLocation: NSObject {
/// 停止定位
public func stopLocation() {
if !isEnable {
return
}
switch locationType {
case .system:
systemLocation.stopUpdatingLocation()
......@@ -104,14 +130,23 @@ extension MRLocation {
private func setupSystemLocation() {
systemLocation.delegate = self
systemLocation.requestWhenInUseAuthorization()
if Bundle.main.infoDictionary?["NSLocationWhenInUseUsageDescription"] != nil {
systemLocation.requestWhenInUseAuthorization()
} else if Bundle.main.infoDictionary?["NSLocationAlwaysUsageDescription"] != nil {
systemLocation.requestAlwaysAuthorization()
}
systemLocation.distanceFilter = 5
systemLocation.desiredAccuracy = 300//300米
}
private func invokeCallbacks(with location: CLLocation?, error: Error?) {
callbacks = callbacks.filter({ c in
c.callback(location, error)
return c.isStay
stayCallbacks.forEach({
$0.callback(location, error)
})
callbacks.forEach({
$0.callback(location, error)
})
callbacks = [LocationCallback]()
}
}
......@@ -119,23 +154,27 @@ extension MRLocation {
extension MRLocation: CLLocationManagerDelegate {
public func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
invokeCallbacks(with: nil, error: error)
_status = .idle
if stayCallbacks.count == 0 {
stopLocation()
_status = .idle
}
}
public func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
lastLocation = locations.last
invokeCallbacks(with: locations.last, error: nil)
_status = .idle
if stayCallbacks.count == 0 {
stopLocation()
_status = .idle
}
}
}
// MARK: 定位回调
public final class LocationCallback: NSObject {
var callback: MRLocation.MRLocationSystemFinish
var isStay: Bool
public init(callback: @escaping MRLocation.MRLocationSystemFinish, isStay: Bool) {
public init(callback: @escaping MRLocation.MRLocationSystemFinish) {
self.callback = callback
self.isStay = isStay
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment