【问题标题】:Core location gives old location whenever comes from background核心位置在来自后台时提供旧位置
【发布时间】:2012-01-28 19:45:13
【问题描述】:

我在 VC 中有一个 CoreLocation 管理器,当用户按下“获取方向”按钮时,我会初始化位置管理器,然后应用会打开带有当前位置和一些预定义目的地位置的谷歌地图方向。

这是我的问题,如果应用程序未处于后台状态,则当前位置几乎总是正确的,但如果应用程序从同一 VC 中的后台调用并且用户再次按下“获取方向”按钮,则当前位置通常显示旧位置。简而言之,我对多任务处理感到困扰,检索位置的时间戳并没有解决我的问题。

IBAction:

   if ( self.locationManager ) {
        [_locationManager release];
        self.locationManager = nil;
    }
    self.locationManager = [[CLLocationManager alloc] init];
    self.locationManager.delegate = self;
    self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    self.locationManager.distanceFilter = kCLDistanceFilterNone;

    self.locationTimer = [NSTimer scheduledTimerWithTimeInterval:LOCATION_TIMER target:self selector:@selector(stopUpdatingLocationTimer) userInfo:nil repeats:NO];
    HUD = [MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
    [self.locationManager startUpdatingLocation]; 

核心位置代表:

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {


NSTimeInterval locationAge = -[newLocation.timestamp timeIntervalSinceNow];
NSLog(@"%f",locationAge);
if (locationAge > 3.0) 
    return;

if (newLocation.horizontalAccuracy < 0) 
    return;

if ( self.currentLocation == nil || self.currentLocation.horizontalAccuracy > newLocation.horizontalAccuracy ) {

    self.currentLocation = newLocation;

    if (self.currentLocation.horizontalAccuracy <= self.locationManager.desiredAccuracy) {

        [self stopUpdatingLocations:YES];

    }

}

}

【问题讨论】:

    标签: ios core-location cllocationmanager


    【解决方案1】:

    在您的示例中,locationAge 是自 timestampnewLocation 以来的秒数的负数表示。这意味着locationAge 永远不会大于 3,并且您实际上是在让每个更新都通过筛子。

    这样设置locationAge

    NSTimeInterval locationAge = [newLocation.timestamp timeIntervalSinceNow];
    

    【讨论】:

    • 马克,我在苹果的 LocateMe 示例代码中看到它,我复制了: NSTimeInterval locationAge = -[newLocation.timestamp timeIntervalSinceNow]; if (locationAge > 5.0) 返回;那么,我们说是不是错了?
    • 哦,对了,他们使用的是-timeIntervalSinceNow,所以它将是一个负值,然后被附加的负值反转。 (这对我来说似乎完全是倒退)你怎么知道你正在获得旧的更新?你确定它们已经超过 3 秒了吗?
    • 我 100% 确定,因为,我用汽车进行了测试,首先我打开了应用程序并获得了一些正确的预定义地点的方向,然后我行驶了大约 2-2.5 公里,然后我再次启动应用程序(从后台状态),它给了我错误的位置,这是旧的。我这样测试了3-4次,都错了。
    • 您是否可能只是看到应用退出前的最后一个位置,而新位置并不能立即满足您的任何准确性要求?您正在检查最佳准确性...
    • 但是我也使用 NSTimer 来停止更新,即使是新的位置也不符合精度要求,我给它分配了 currentLocation 变量,正如你在代码 sn-p 中看到的那样,我预计坐标值的变化很小至少,但我很倒霉。
    【解决方案2】:

    遇到同样问题的朋友,

    另外,一些与网络核心位置相关的教程,引导我解决这个问题。

    当然,每当 CLLocation ivar 时,我都会在我的 VC 中保留 CLLocation ivar 集和谷歌地图调用,我的应用程序进入后台。

    然后,我的应用程序由用户从后台调用,并开始更新位置,
    并且旧的 CLLocation ivar 不是 nil 并且可能是最好的水平精度 新来的。因此;

    if ( self.currentLocation == nil || self.currentLocation.horizontalAccuracy > newLocation.horizontalAccuracy )
    

    这条线会导致问题,并且 CLLocation ivar 中的旧位置 从未更换过。

    所以我像这样更改了 viewDidDisappear 并将 nil 值分配给 CLLocation 变量并完美运行。

    - (void)viewDidDisappear:(BOOL)animated
    {
        [super viewDidDisappear:animated];
    
        [self.locationManager stopUpdatingLocation]; // CLLocationManager
        self.locationManager = nil; 
        [self.locationTimer invalidate]; // NSTimer
        self.locationTimer = nil;
        self.currentLocation = nil; // CLLocation 
    }
    

    ps:谢谢马克亚当斯

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-09
      • 1970-01-01
      相关资源
      最近更新 更多