我正在處理定期獲取用戶位置的應用程序。最大的問題是,有時應用程序卡住了,沒有更多的更新傳遞。即使我(殺死和)重新啓動我的應用程序,沒有任何變化。 (爲位置管理器設置的精度值接近100-200米。) 但是,當我啓動Google地圖應用程序時,幾秒鐘內就會得到一個非常準確的位置(如果我切換回來,它會傳送到我的應用程序) 。爲什麼? 以下是一些相關的代碼部分:CLLocationmanager有時卡住
定時器週期性地調用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] ;
}
}
的啓動和停止方法簡單verifiy如果是的LocationManager爲零,如果是他們叫createManager
,然後調用啓動& 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 *)經理\t didFailWithError:(NSError *)錯誤發生時,然後重新啓動您的CLManager。這比定期檢查狀態好多了(我認爲這是你的方法) – Yorxxx 2012-02-16 11:59:59
我也使用該方法,但僅用於監視錯誤。當定時器更新成功後觸發定時器,它將設置等待標誌。它可以在不獲得更新的情況下多次觸發(例如每3秒),在這種情況下,我不需要啓動位置管理器,並且該標誌指示它現在正在更新/定位。 – Templar 2012-02-16 13:06:52