11

Location Services在我的一些应用程序中使用。我在我的方法中使用了locationManager:didUpdateToLocation:fromLocation:一种方法来过滤掉错误、不准确或太远的位置。并尽量减少gps“抖动”。这是我使用的:

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

有没有人对此方法有任何改进以使其更准确?66是不是太高了horizontalAccuracy,会收到很多无效的位置?我应该降低这个吗?

有没有办法摆脱 iPhone 上的 gps 带来的“抖动”?

4

3 回答 3

7

除了这些,我还使用了一个:

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

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

实际上我拥有的最后一个有效位置在哪里self.lastKnownLocation,它是:

@property(nonatomic, copy) CLLocation *lastKnownLocation;
于 2012-05-24T17:17:51.047 回答
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....**
       }          
    }           
 }  
于 2012-12-20T12:54:04.547 回答
1

你可以签出这个

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

}
于 2017-08-16T08:55:35.853 回答