我在我的一些应用程序中使用了定位服务
。我在我的 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 带来的“抖动”?
最佳答案
除了这些我还用了一个:
if(self.lastKnownLocation)
{
CLLocationDistance dist = [newLocation distanceFromLocation:self.lastKnownLocation];
if(dist > newLocation.horizontalAccuracy)
{
//.....
}
}
self.lastKnownLocation
实际上是我拥有的最后一个有效位置,它是:
@property(nonatomic, copy) CLLocation *lastKnownLocation;
关于objective-c - 用于过滤掉无效/不准确位置的 iOS 位置服务方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10742156/