iphone - 核心位置第一点无效

标签 iphone objective-c ios core-location cllocationmanager

我使用 Core Location 的第一个位置几乎总是无效的。

我的代码如下:

- (void)locationManager:(CLLocationManager *)manager
    didUpdateToLocation:(CLLocation *)newLocation
           fromLocation:(CLLocation *)oldLocation
{ 
    //for saving the data ACCURATELY for calculations
    // make sure the coordinates are valid
    if ([self isValidLocation:newLocation withOldLocation:oldLocation])
    {         
        mDistance = [newLocation distanceFromLocation:oldLocation];

        [[NSNotificationCenter defaultCenter]postNotificationName:@"locationUpdated" object:nil];
    }
}

如您所见,它使用 isValidLocation 检查它是否是有效位置。该代码在这里:

- (BOOL)isValidLocation:(CLLocation *)newLocation
        withOldLocation:(CLLocation *)oldLocation
{    
    // Throw away first point
    if (isFirstPoint)
    {
        NSLog(@"First point thrown away.");
        isFirstPoint = NO; //subsequent updates will NOT be the first point
        return NO;
    }
    // Filter out nil locations
    if (!newLocation){
        NSLog(@"New location invalid");
        return NO;
    }
    if (!oldLocation)
    {
        NSLog(@"Old location invalid");
        return NO;
    }
    // Filter out points by invalid accuracy
    if (newLocation.horizontalAccuracy < 0)
    {
        return NO;
    }
    // Filter out points that are out of order
    NSTimeInterval secondsSinceLastPoint = [newLocation.timestamp 
                                            timeIntervalSinceDate:oldLocation.timestamp];
    if (secondsSinceLastPoint < 0){
        return NO;
    }
    // Filter out points created before the manager was initialized
    NSTimeInterval secondsSinceManagerStarted = [newLocation.timestamp 
                                                    timeIntervalSinceDate:locationManagerStartDate];
    if (secondsSinceManagerStarted < 0){
        return NO;
    }
    // If the distance is negative
    if ([newLocation distanceFromLocation:oldLocation] < 0)
    {
        return NO;
    }
    if(newLocation.speed < 0) 
    {
        return NO;
    }
    // GIANT ELSE: The newLocation is good to use
    return YES;
} 

即使检查了所有这些,我的第一点仍然无效。例如,我下类回家,在家里打开我的位置管理器,但我的第一个坐标来 self 工作的办公室。我该如何解决这个问题?

这是当我得到一个无效的第一个点时会发生什么: enter image description here

最佳答案

这实际上取决于您的用例。 Core Location 通常会返回设备的最后已知位置,以便尽快为您提供结果。在设备完成获取新位置之前,会将此位置传递给您。最后一个已知位置可能与您当前位置相差几公里。

根据您应用的类型,这可能是一个完全可以忽略的差异(例如,如果应用只是想定位用户所在的国家/地区),或者它可能代表一个 Not Acceptable 错误。核心位置无法分辨。

决定如何处理这种行为对您来说很重要。始终丢弃第一个报告的位置可能不是最好的策略。您可能希望在调用 startUpdatingLocation 时存储时间,并且只有在系统在非常短的时间内(例如,1/10 秒)将第一个报告的位置传递给您时才将其丢弃这使得它很可能是一个缓存位置。此外,位置的 timestamp 属性可能有助于判断报告位置的(不)准确度。

关于iphone - 核心位置第一点无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8040262/

相关文章:

iphone - 从 JSON 中为 UITableView 排序 NSDictionary

iphone - 有没有办法取消动画 UITableView/UIScrollView setContentOffset :animated:?

iphone - 如何更改布局 View Controller ?

iPhone - 应用内购买 : recording the purchase

objective-c - 使用 extern 作为静态变量

iphone - 检查 bundle 中是否存在图像 - iPhone

iphone - 可滚动位置后窗口的背面

iphone - 为什么 fabs 返回整数?

ios - 如何在 UINavigatorBar/Button 中出现 "Back button style"

iphone - 代码正确时,UILabel 中的删除键不起作用