用于过滤无效/不准确位置的iOS位置服务方法

问题描述 投票:11回答:3

我在一些应用程序中使用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所带来的“抖动”?

objective-c ios gps core-location cllocationmanager
3个回答
7
投票

除了这些,还有一个我用的:

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

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

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

@property(nonatomic, copy) CLLocation *lastKnownLocation;

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

}
© www.soinside.com 2019 - 2024. All rights reserved.