2012-02-11 101 views
0

我想寫一個方法來返回當前位置。當我等待30秒,1分鐘或2分鐘的時間(在設置中設置)時,我會顯示一個警報視圖。我允許用戶繞過定時器,並等待一個alertView按鈕,接受我將在警報視圖中顯示和更新的當前精度。我的代碼有幾個洞,我需要幫助。本質上,它是知道如何讓getCurrentLocation方法等待定時器。我不能只是使用延遲,因爲我打算強制計時器在用戶點擊按鈕或定位精度滿足時(這在定時器中檢查)過期。下面是代碼:如何在返回位置之前等待計時器?

- (void)timerFireMethod { 
    static int elapsedTime = 0; 
    elapsedTime++; 
    if (elapsedTime >= gpsTimeout) { 
     [locationManager stopUpdatingLocation]; 
     elapsedTime = 0; // reset static variable 
     // !!! How do I do the following !!! 
     // do something to allow getCurrentLocation to return 
    } 
    if ((currentLocation.horizontalAccuracy <= gpsDesiredAccuracy) & (currentLocation.verticalAccuracy <= 2*gpsDesiredAccuracy)) { 
     [locationManager stopUpdatingLocation]; 
     elapsedTime = 0; // reset static variable 
     // do something to allow getCurrentLocation to return 
    } 
} 

- (CLLocation *)getCurrentLocation { 
    // check if current accuracy is good enough and return if true 
    if ((currentLocation.horizontalAccuracy <= gpsDesiredAccuracy) & (currentLocation.verticalAccuracy <= 2*gpsDesiredAccuracy)) { 
     [locationManager stopUpdatingLocation]; 
     return currentLocation; 
    } else { 
     // show alert with count down timer 
     UIAlertView *gpsAlertView = [[UIAlertView alloc] initWithTitle:nil message:@"tbd put in timer and list location accuracy updates" delegate:self cancelButtonTitle:@"Continue With Current Accuracy" otherButtonTitles:nil]; 
     [gpsAlertView show]; 
     // start timer 
     NSTimer *gpsTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 
                 target:self selector:@selector(timerFireMethod:) 
                 userInfo:nil repeats:YES]; 

     // !!! How do I do the following !!! 
     // wait for when timer expires, 
     // the user to press "Continue With Current Accuracy" button (can force timer to expire?) 
     return currentLocation; 
    } 
} 

回答

1

伺候任何計時器或任何加速度計或用戶事件是簡單地用一個return語句退出當前的方法回到UI運行循環的方式。

任何你想在等待完成後可以進入定時器或事件回調方法。

+0

這似乎是有道理的。通過與沒有GPS相同的方式,我被困在我的應用程序中將gps功能鎖定到我的應用程序中。我會在接下來的一兩天內處理這個問題並報告它是如何發生的。 – 2012-02-12 06:12:20

+0

立即開始運行。你的建議解決了。我還在Apple的LocateMe示例代碼中找到了很好的參考。 – 2012-02-15 02:58:36

相關問題