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

标签 objective-c ios gps core-location cllocationmanager

我在我的一些应用程序中使用了定位服务。我在我的 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

有没有人对这种方法有任何改进以使其更准确? 66horizo​​ntalAccuracy 是否太高并且会收到很多无效位置?我应该降低这个吗?

有没有办法消除 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/

相关文章:

android - React Native Android 位置请求超时

ios - 你什么时候会使用 NSNumber 文字来创建封装的字符值?

iphone - 使用 CATransition 从当前位置滑动图像 400px

ios - UITableView 动态单元格高度仅在滚动后才正确

Node.js GPS 设备跟踪性能注意事项

android - 以编程方式打开/关闭移动数据和 GPS - Android 5.0 以上

objective-c - 如何检查用户选择的文件我的沙盒应用程序是否有权写入?

objective-c - 内存管理与方法调用

ios - 解析不注册推送通知设备

ios - iOS 9.0 之前的 UIStackView