2017-01-12 43 views
1

我使用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 

任何解決方案? 提前致謝。

+0

我沒有看到任何生成'distance:'log的代碼。 – matt

+0

我沒有在上面添加它,因爲它有一些機密數據 –

回答

0

您可能只是希望得到比可以交付的更多的準確性。首先,手機最初使用的是除GPS之外的其他車載傳感器。其次,GPS系統本身並不特別準確。因此,即使你是靜止的,你也可能期望手機認爲你的位置隨着時間的推移而發生微小的變化,這是因爲不同的傳感器變暖了,並且僅僅因爲最精確的傳感器GPS並不是那麼準確。

+0

有沒有我可以添加的任何測試來避免它? –

+0

當我的設備不移動時,我需要穩定的距離 –

+0

「當我的設備不移動時,我需要穩定的距離」我告訴你,你不一定會得到它。你有沒有坐在地圖應用程序或谷歌地圖上看到你的位置,並且看到了位置偏移?我當然有。這只是事物的本質。 – matt