2012-05-24 60 views
10

我在一些應用程序中使用Location Services。我在我的locationManager:didUpdateToLocation:fromLocation:方法中使用了一種方法來過濾不良,不準確或太遠的位置。並儘量減少GPS「抖動」。這裏是我使用的:iOS位置服務過濾無效/不準確位置的方法

/** 
* Check if we have a valid location 
* 
* @version $Revision: 0.1 
*/ 
+ (BOOL)isValidLocation:(CLLocation *)newLocation withOldLocation:(CLLocation *)oldLocation { 

    // Filter out nil locations 
    if (!newLocation) return NO; 

    // Filter out points by invalid accuracy 
    if (newLocation.horizontalAccuracy < 0) return NO; 
    if (newLocation.horizontalAccuracy > 66) return NO; 

    // Filter out points by invalid accuracy 
    #if !TARGET_IPHONE_SIMULATOR 
    if (newLocation.verticalAccuracy < 0) return NO; 
    #endif 

    // Filter out points that are out of order 
    NSTimeInterval secondsSinceLastPoint = [newLocation.timestamp timeIntervalSinceDate:oldLocation.timestamp]; 
    if (secondsSinceLastPoint < 0) return NO; 

    // Make sure the update is new not cached 
    NSTimeInterval locationAge = -[newLocation.timestamp timeIntervalSinceNow]; 
    if (locationAge > 5.0) return NO; 

    // Check to see if old and new are the same 
    if ((oldLocation.coordinate.latitude == newLocation.coordinate.latitude) && (oldLocation.coordinate.longitude == newLocation.coordinate.longitude)) 
     return NO; 

    return YES; 

}//end 

有沒有人有任何改善這種方法,使其更加準確?是過高的horizontalAccuracy會收到很多無效的位置?我應該降低這個嗎?

有沒有辦法讓iPhone上的gps「抖動」

+0

我開始對類似的工作。我正在考慮實施卡爾曼濾波器,但我不確定這是否會被殺死。我很想看到人們在這個問題上的答案。 – scord

+0

是的,我也看了一個卡爾曼濾波器,但是這個數學很瘋狂。 –

回答

6

除了這些還有一個我用:

if(self.lastKnownLocation) 
{ 
    CLLocationDistance dist = [newLocation distanceFromLocation:self.lastKnownLocation]; 

    if(dist > newLocation.horizontalAccuracy) 
    { 
      //..... 
    } 
} 

哪裏self.lastKnownLocation實際上是最後一個有效位置,我已經和它是一個:

@property(nonatomic, copy) CLLocation *lastKnownLocation; 
+0

這看起來很不錯。你用什麼來獲得可接受的水平準確度? –

+0

0到60 ..我正在實施步行跟蹤,到目前爲止,我已經對這個和你發佈的結果有很好的結果。 – graver

+0

你有問題只接受<60的地點嗎?我收到用戶的報告,我的應用從來沒有找到他們的位置,因爲他們的水平準確度顯然從未低於60.我應該刪除這個檢查嗎? –

5
-(void)locationManager:(CLLocationManager *)manager 
    didUpdateToLocation:(CLLocation *)newLocation 
      fromLocation:(CLLocation *)oldLocation 
{ 
if (!newLocation) 
    { 
     NSLog(@"Filter out points by invalid accuracy"); 
     return; 
    } 

    // Filter out points by invalid accuracy 
    if (newLocation.horizontalAccuracy < 0) 
    { 
     return; 
    } 
    if(oldLocation.coordinate.latitude>90 && oldLocation.coordinate.latitude<-90 && oldLocation.coordinate.longitude>180 && oldLocation.coordinate.longitude<-180) 
    { 
     NSLog(@"old"); 
     return; 
    } 
    if(newLocation.coordinate.latitude>90 || newLocation.coordinate.latitude<-90 || newLocation.coordinate.longitude>180 || newLocation.coordinate.longitude<-180) 
    { 
     NSLog(@"new"); 
     return; 
    } 

    /////// 
    NSDate *eventDate=newLocation.timestamp; 
    NSTimeInterval eventinterval=[eventDate timeIntervalSinceNow]; 


    if (abs(eventinterval)<30.0) 
    {   
     if (newLocation.horizontalAccuracy>=0 && newLocation.horizontalAccuracy<20) 
     { 
      **//finally you are getting right updated value here....** 
     }   
    }   
} 
0

您可以檢出該

https://medium.com/@mizutori/make-it-even-better-than-nike-how-to-filter-locations-tracking-highly-accurate-location-in-774be045f8d6

的主要思想是,以文件服務器上的數據的方法didUpdateLocation:

過濾方法就是這樣的:

func filterAndAddLocation(_ location: CLLocation) -> Bool{ 
    let age = -location.timestamp.timeIntervalSinceNow 

    if age > 10{ 
     return false 
    } 

    if location.horizontalAccuracy < 0{ 
     return false 
    } 

    if location.horizontalAccuracy > 100{ 
     return false 
    } 

    locationDataArray.append(location) 

    return true 

}