为节省电量,我想每隔30秒拨打requestLocation()
以获取新位置。
DispatchQueue.main.asyncAfter(deadline: .now() + 30) {
self.requestLocation()
}
但我这里有问题。被调用的requestLocation()
,didUpdateLocations
,但CLLocation
并未一直改变。
附:我在模拟器中使用Freeway Drive
<+37.42467819,-122.22918858> +/- 5.00m (speed 35.86 mps / course 311.13) @ 29.01.2018, 12:00:10 Восточная Европа, стандартное время
<+37.42467819,-122.22918858> +/- 5.00m (speed 35.86 mps / course 311.13) @ 29.01.2018, 12:00:40 Восточная Европа, стандартное время
<+37.42467819,-122.22918858> +/- 5.00m (speed 35.86 mps / course 311.13) @ 29.01.2018, 12:01:10 Восточная Европа, стандартное время
如果我在没有截止日期前只拨打requestLocation()
,那么所有工作都会完美无缺。
你有什么想法我做错了吗?
答案 0 :(得分:0)
当您拨打CLLocationManager
startUpdatingLocation
方法时,CLLocationManager
将更新位置,直到您调用停止搜索位置的stopUpdatingLocation
方法
假设您的requestLocation()
方法正在调用manager.startUpdatingLocation()
,其中经理是var manager = CLLocationManager()
,您需要在CLLocationManagerDelegate
方法{{1}收到有效位置时停止请求位置调用你的代码将是这样的
didUpdateLocations
答案 1 :(得分:0)
请参阅此(您的值的更改时间参数)
internal func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
if let newLocation = locations.last
{
let locationAge = -newLocation.timestamp.timeIntervalSinceNow
if locationAge > 5.0
{
return
}
if newLocation.horizontalAccuracy < 0
{
return
}
}
}
答案 2 :(得分:0)
self.locationManager = CLLocationManager()
guard let locationManager = self.locationManager else {
return
}
if CLLocationManager.authorizationStatus() == .notDetermined {
// you have 2 choice
// 1. requestAlwaysAuthorization
// 2. requestWhenInUseAuthorization
locationManager.requestAlwaysAuthorization()
}
locationManager.desiredAccuracy = kCLLocationAccuracyBest // The accuracy of the location data
locationManager.distanceFilter = 200 //The minimum distance (measured in meters) a device must move horizontally before an update event is generated.
因此,将locationManager上的distanceFilter更改为最小距离将调用委托而不会超时。