Commit 15accfaa by Sarkizz

优化定位逻辑

parent f113e76f
...@@ -37,8 +37,6 @@ public class MRLocation: NSObject { ...@@ -37,8 +37,6 @@ public class MRLocation: NSObject {
return _status return _status
} }
} }
/// 根据状态判断是否定位的回调
public var authorization: MRlocationAuthorization?
/// 最后一次获取的定位 /// 最后一次获取的定位
public var lastLocation: CLLocation? public var lastLocation: CLLocation?
...@@ -54,29 +52,54 @@ public class MRLocation: NSObject { ...@@ -54,29 +52,54 @@ public class MRLocation: NSObject {
} }
private var _status: LocationStatus = .idle private var _status: LocationStatus = .idle
private var callbacks = [LocationCallback]() private var callbacks = [LocationCallback]()
private var stayCallbacks = [LocationCallback]()
public convenience init(type: LocationType = .system) { public convenience init(type: LocationType = .system) {
self.init() self.init()
locationType = type 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 finish: 定位回调
/// - Parameter stay: 是否常驻,若常驻,则这个回调在每次定位后都会调用 /// - Parameter stay: 是否常驻,若常驻,则这个回调在每次定位后都会调用
/// - Parameter force: 是否强制定位。强制定位会无视当前状态直接调用定位函数 /// - Parameter force: 是否强制定位。强制定位会无视当前状态直接调用定位函数
public func startLocation(finish: MRLocationSystemFinish? = nil, public func startLocation(finish: MRLocationSystemFinish? = nil,
authorization: MRlocationAuthorization? = nil,
stay: Bool = false, force: Bool = false) { stay: Bool = false, force: Bool = false) {
if !isEnable {
return
}
let isStartLocating = authorization?(CLLocationManager.authorizationStatus()) ?? true let isStartLocating = authorization?(CLLocationManager.authorizationStatus()) ?? true
if isStartLocating { if isStartLocating {
/// 存储回调 /// 存储回调
if let finish = finish { if let finish = finish {
let c = LocationCallback(callback: finish, isStay: stay) let c = LocationCallback(callback: finish)
callbacks.append(c) if stay {
stayCallbacks.append(c)
} else {
callbacks.append(c)
}
} }
if _status != .locating || force { if _status != .locating || force {
switch locationType { switch locationType {
case .system: case .system:
systemLocation.stopUpdatingLocation()
systemLocation.startUpdatingLocation() systemLocation.startUpdatingLocation()
_status = .locating _status = .locating
} }
...@@ -86,6 +109,9 @@ public class MRLocation: NSObject { ...@@ -86,6 +109,9 @@ public class MRLocation: NSObject {
/// 停止定位 /// 停止定位
public func stopLocation() { public func stopLocation() {
if !isEnable {
return
}
switch locationType { switch locationType {
case .system: case .system:
systemLocation.stopUpdatingLocation() systemLocation.stopUpdatingLocation()
...@@ -104,14 +130,23 @@ extension MRLocation { ...@@ -104,14 +130,23 @@ extension MRLocation {
private func setupSystemLocation() { private func setupSystemLocation() {
systemLocation.delegate = self 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?) { private func invokeCallbacks(with location: CLLocation?, error: Error?) {
callbacks = callbacks.filter({ c in stayCallbacks.forEach({
c.callback(location, error) $0.callback(location, error)
return c.isStay
}) })
callbacks.forEach({
$0.callback(location, error)
})
callbacks = [LocationCallback]()
} }
} }
...@@ -119,23 +154,27 @@ extension MRLocation { ...@@ -119,23 +154,27 @@ extension MRLocation {
extension MRLocation: CLLocationManagerDelegate { extension MRLocation: CLLocationManagerDelegate {
public func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) { public func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
invokeCallbacks(with: nil, error: error) invokeCallbacks(with: nil, error: error)
_status = .idle if stayCallbacks.count == 0 {
stopLocation()
_status = .idle
}
} }
public func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { public func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
lastLocation = locations.last lastLocation = locations.last
invokeCallbacks(with: locations.last, error: nil) invokeCallbacks(with: locations.last, error: nil)
_status = .idle if stayCallbacks.count == 0 {
stopLocation()
_status = .idle
}
} }
} }
// MARK: 定位回调 // MARK: 定位回调
public final class LocationCallback: NSObject { public final class LocationCallback: NSObject {
var callback: MRLocation.MRLocationSystemFinish 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.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