Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
zhangyongji
/
MRFramework
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Members
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
15accfaa
authored
Oct 22, 2019
by
Sarkizz
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
优化定位逻辑
parent
f113e76f
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
52 additions
and
13 deletions
+52
-13
MRFramework/MRFramework/FoundationManagers/Location/MRLocation.swift
+52
-13
No files found.
MRFramework/MRFramework/FoundationManagers/Location/MRLocation.swift
View file @
15accfaa
...
...
@@ -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
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment