【发布时间】:2012-03-07 17:32:02
【问题描述】:
我正在开发一个定期获取用户位置的应用程序。最大的问题是有时应用程序会卡住,不再提供更新。即使我(杀死并)重新启动我的应用程序,也没有任何改变。 (为位置管理器设置的准确度值接近 100-200 米。) 但是,当我启动谷歌地图应用程序时,它会在几秒钟内获得一个非常准确的位置(如果我切换回来,它会传送到我的应用程序)。为什么 ? 以下是一些相关的代码部分:
timerFiredAction 由计时器定期调用。
-(void) timerFiredAction
{
if (isStillWaitingForUpdate)
{
successiveTimerActivationCount ++;
// force LM restart if value too big , e.g. 30 ( stop + start )
return;
}
successiveTimerActivationCount = 0 ;
isStillWaitingForUpdate = YES;
/* isRecordingX is always true */
if (isSignificant && isRecordingSig) [self startSignificant ];
if (isGPS && isRecordingGPS) [self startGps];
}
// this is called in delegate method only
-(void) timerStopLocationServices
{
isStillWaitingForUpdate = NO;
if (isGPS) [ self stopGps] ;
if (isSignificant) [self stopSignificant];
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
// verify accuracy and stuff
if ( isStillWaitingForUpdate && _other_validations_ )
{
// store it
[self timerStopLocationServices] ;
}
}
start 和 stop 方法只是验证 locationmanager 是否为 nil,如果是,则调用 createManager 然后调用 start 和 stopUpdatingLocation。
LM 的创建如下所示:
-(void)createManager
{
@synchronized (self)
{
if (locationManager != nil) {
[self releaseManager]; // stop timer, stop updating , reelase previous if exists
}
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
double desired;
// accuracy is an init param, snap to one smaller constant
// generally 100-200
if (accuracy >= kCLLocationAccuracyThreeKilometers) desired = kCLLocationAccuracyThreeKilometers; else
if (accuracy >= kCLLocationAccuracyKilometer) desired = kCLLocationAccuracyKilometer; else
if (accuracy >= kCLLocationAccuracyHundredMeters) desired = kCLLocationAccuracyHundredMeters; else
if (accuracy >= kCLLocationAccuracyNearestTenMeters) desired = kCLLocationAccuracyNearestTenMeters; else
if (accuracy >= kCLLocationAccuracyBest) desired = kCLLocationAccuracyBest; else
if (accuracy >= kCLLocationAccuracyBestForNavigation) desired = kCLLocationAccuracyBestForNavigation;
locationManager.desiredAccuracy = desired;
locationManager.distanceFilter = distanceFilter;
}
}
有没有人经历过这样的事情?欢迎任何想法:) 谢谢!
【问题讨论】:
-
我对触发的动作有点困惑。你为什么要这样做?发生错误时有 (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error 然后重新启动您的 CLManager。这是比定期检查状态更好的方法(我认为这是你的方法)
-
我也使用该方法,但仅用于监控错误。当定时器在成功更新后触发时,它会设置等待标志。它可以多次触发而不获取更新(例如每 3 秒),在这种情况下,我不需要(?)启动位置管理器,并且标志表明它现在正在更新/定位。
标签: iphone ios google-maps core-location cllocationmanager