【发布时间】: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