是否可以在Timer
内调用didUpdateLocations
?我的意思是......定位器在位置管理器
_timer = Timer.scheduledTimer(timeInterval: 10, target: self, selector: #selector(_:didUpdateLocations:)), userInfo: nil, repeats: true)
这是否正常工作?如果是,那么它将每隔10秒更新一次位置?
是否可以在Timer
内调用didUpdateLocations
?我的意思是......定位器在位置管理器
_timer = Timer.scheduledTimer(timeInterval: 10, target: self, selector: #selector(_:didUpdateLocations:)), userInfo: nil, repeats: true)
这是否正常工作?如果是,那么它将每隔10秒更新一次位置?
您无法调用didUpdateLocations。如果你想频繁的更新,您最好的选择是在标准位置服务:
的标准位置的服务是最适合的应用程序, 直接提供与位置相关的信息给用户,但它可能 通过其他类型的使用的应用程序。要启动该服务,请配置 期望的位置 管理器的准确度和距离过滤器属性,然后调用requestLocation(),startUpdatingLocation(), 或allowDeferredLocationUpdates(untilTraveled:timeout :)方法。从不 指定的精度值比您需要的大。核心位置 使用您指定的精度值更好地管理电源。精度要求更高的精度要求更精确的硬件,如GPS,其中 消耗更多的功率。
https://developer.apple.com/reference/corelocation/cllocationmanager
你的回调选择必须有一个不同的签名:
class MyClass {
func startTimer() {
_timer = Timer.scheduledTimer(timeInterval: 10, target: self, selector: #selector(MyClass.timerCallBack(_:)), userInfo: nil, repeats: true)
}
// ...
func timerCallBack(timer: Timer) {
// Here you go.
}
}
(可能包含一些语法错误;目前不具备的Xcode在手)
是的,我知道它可能会调用做smith的函数,但是正是我想要的是一段时间后的用户位置 – yerpy
你不能这样做。你可以在位置管理器上调用'requestLocation',但是这会触发对你的委托'locationManager(_:didUpdateLocations:)'实现的_async_调用。没有_synced_这样做的方法 –
不要基于时间间隔的位置,因为这不是赌注的方式,你可能会消耗大量的电池。最好监控用户行走或驾驶的动作(使用coremotion框架),并根据距离过滤器获取位置,每隔10米或更长时间进行设置。当用户处于静止状态时,请确保暂停您的位置更新,或者将位置管理器的期望精度设置为3公里,并将距离过滤器设置为更高的值。 –