我开始使用ios-5开发一个项目,我想在每15分钟找到当前位置。
这是我的代码
- (void)applicationDidEnterBackground:(UIApplication *)application {
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters;
[locationManager startUpdatingLocation];
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
NSDate *newLocationTimestamp = newLocation.timestamp;
NSDate *lastLocationUpdateTiemstamp;
int locationUpdateInterval = 60; //1min
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
if (userDefaults) {
lastLocationUpdateTiemstamp = [userDefaults objectForKey:@"myevent"];
if (!([newLocationTimestamp timeIntervalSinceDate:lastLocationUpdateTiemstamp] < locationUpdateInterval)) {
NSLog(@"New Location: %@", newLocation);
//[self locationManager:locationManager didUpdateToLocation:newLocation fromLocation:oldLocation];
[userDefaults setObject:newLocationTimestamp forKey:@"myevent"];
}
}
}我遇到的问题是这个代码在后台工作,但我每隔5分钟就得到一次当前位置(这里是newLocation),而不是在1分钟之后,即使我设置了locationUpdateInterval = 60sec
我已经尝试了所有的方法,但没有得到确切的解决方案。提前感谢!
发布于 2011-12-26 20:47:32
我听到的轶事证据是,为了节省电池寿命,CoreLocation更新只会周期性地触发后台应用程序。
我在这里看到了一些关于这个主题的相关问题,like this one。
另外,请看一下Apple's Location Awareness Programming Guide,特别是“启动重要变化的位置服务”部分。
https://stackoverflow.com/questions/8635781
复制相似问题