2011-12-27 86 views
2

来了,我有VC一CoreLocation经理,当用户按下“获取路线”按钮,我initalize位置管理和应用程序打开谷歌地图方向与当前位置和一些预定义的目标位置。核心位置,让原来的位置,每当从背景

这是我的问题,如果应用程序不处于后台状态,当前位置几乎总是如此,如果应用程序从同一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]; 

    } 

} 

}

回答

0

在您的例子locationAge是由于newLocationtimestamp的秒数的负表示。这意味着,locationAge永远不会大于3,你就有效地使通过筛板每次更新。

设置locationAge这样的:

NSTimeInterval locationAge = [newLocation.timestamp timeIntervalSinceNow]; 
+0

马克,我看到它在苹果的LocateMe示例代码,我复制:NSTimeInterval locationAge = - [newLocation.timestamp timeIntervalSinceNow] if(locationAge> 5.0)return;那么,我们说这是错的?他们使用'-timeIntervalSinceNow'所以这将是一个负值,然后通过附加的负极接反 – ubaltaci 2011-12-27 23:16:44

+0

哦,对了。 (这似乎完全屁股向我)你怎么知道你正在得到旧的更新?你确定他们已经超过3秒了吗? – 2011-12-27 23:24:20

+0

我100%肯定,因为,我对赛车进行测试,首先我打开应用程序,并获得方向一些预先定义的场地是正确的,那么我前往appromixately 2-2.5公里,然后我再次启动的应用程序(从后台状态),它给了我错误的位置,这是旧的。我已经测试过3-4次,都是错的。 – ubaltaci 2011-12-27 23:29:06

0

对于那些谁遇到同样的问题,

此外,与网络核心位置相关的一些教程,导致我这个问题。

当然,我保持CLLocation ivar在我的VC中,每当CLLocation ivar 集合和谷歌地图调用,我的应用程序去背景。

然后,我的应用程序由用户从后台调用,并开始更新位置,
老CLLocation伊娃不是零,可能是最好的horizantal准确性然后 新来了。因此;

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

这一行的原因的问题,老位置CLLocation伊娃 从未更换。

因此,我改变viewDidDisappear这样,我分配零值CLLocation变量和完美的作品。

P.S:谢谢你马克·亚当斯