我使用CCLocationManager追蹤每1分鐘場地,這裏是我的didUpdateLocation委託方法:如何過濾無效位置更新的iOS
func changeLocationAccuracy() {
switch locationManager.desiredAccuracy {
case kCLLocationAccuracyBest:
locationManager.desiredAccuracy = kCLLocationAccuracyThreeKilometers
locationManager.distanceFilter = 99999
case kCLLocationAccuracyThreeKilometers:
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.distanceFilter = kCLDistanceFilterNone
default: break
}
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
guard let newLocation = locations.last else {
return
}
let interval = newLocation.timestamp.timeIntervalSinceNow
if abs(interval) > 5 || locationManager.desiredAccuracy != kCLLocationAccuracyBest {
return
}
if newLocation.horizontalAccuracy < 0 {
return
}
if currentLocation != nil && newLocation.timestamp.timeIntervalSince(currentLocation.timestamp) < 0 {
return
}
currentLocation = newLocation
print("interval: \(interval)")
print("desiredAccuracy: \(locationManager.desiredAccuracy)")
print("accuracy: \(newLocation.horizontalAccuracy)")
timer = Timer.scheduledTimer(timeInterval: 60, target: self, selector: #selector(LocationService.changeLocationAccuracy), userInfo: nil, repeats: false)
changeLocationAccuracy()
}
正如你可以看到在上面,我過濾掉無效的位置,但問題是我的距離正在改變,而不移動我的手機。這裏是我的日誌數據:
interval: -0.0140599608421326
desiredAccuracy: -1.0
accuracy: 90.8123422472749
distance : 113.473282606026
-----------
interval: -0.0128610134124756
desiredAccuracy: -1.0
accuracy: 90.8123422472749
distance : 113.473282606026
-----------
interval: -0.00315600633621216
desiredAccuracy: -1.0
accuracy: 2000.0
distance : 180.768761850842
-----------
interval: -0.00381100177764893
desiredAccuracy: -1.0
accuracy: 2000.0
distance : 180.768761850842
-----------
interval: -0.0131250023841858
desiredAccuracy: -1.0
accuracy: 219.000225316427
distance : 0.763731059143118
-----------
interval: -0.0137189626693726
desiredAccuracy: -1.0
accuracy: 219.000225316427
distance : 0.763731059143118
任何解決方案? 提前致謝。
我沒有看到任何生成'distance:'log的代碼。 – matt
我沒有在上面添加它,因爲它有一些機密數據 –