2012-05-24 4 views
10

Ich verwende Location Services in einigen meiner Apps. Ich habe eine Methode, die ich in meiner locationManager:didUpdateToLocation:fromLocation: Methode verwenden, um schlechte, ungenaue oder zu weit entfernte Standorte herauszufiltern. Und um GPS-Jitter zu minimieren. Hier ist, was ich verwende:iOS Location Services Methode zum Filtern ungültiger/ungenauer Standorte

/** 
* 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 

Hat jemand Verbesserungen an dieser Methode, um es genauer zu machen? Ist zu hoch von horizontalAccuracy und wird viele ungültige Standorte erhalten? Soll ich das senken?

Gibt es eine Möglichkeit, den "Jitter" zu bekommen, den GPS auf dem iPhone gibt?

+0

Ich beginne die Arbeit an etwas ähnliches. Ich denke darüber nach, einen Kalman-Filter zu implementieren, bin mir aber nicht sicher, ob das Über-Töten sein wird. Ich bin daran interessiert, die Antworten der Leute auf diese Frage zu sehen. – scord

+0

Ja, ich habe mir auch einen Kalman-Filter angeschaut, aber die Mathematik dafür ist verrückt. –

Antwort

6

Zusätzlich zu diesen gibt es eine weitere ich benutze:

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

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

Wo self.lastKnownLocation tatsächlich der letzte gültige Lage ist, ich habe und es ist ein:

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

Das sieht nach einer guten Ergänzung aus. Was verwendest du für eine akzeptable horizontale Genauigkeit? –

+0

0 bis 60 .. Ich implementiere Walking-Tracking und bisher habe ich gute Ergebnisse mit diesem und denen, die Sie gepostet haben. – graver

+0

Hatten Sie Probleme, nur Orte zu akzeptieren, die <60 sind? Ich habe Berichte von Benutzern, dass meine App ihren Standort nie findet, da ihre horizontale Genauigkeit nie unter 60 fällt. Sollte ich diesen Check insgesamt entfernen? –

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

Sie können diese Kasse

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

Die Hauptidee ist es, die Daten in der Methode didUpdateLocation:

Die Filtermethode ist einfach so Filer:

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 

} 
Verwandte Themen