2014-04-01 42 views
0

我從iPhone的GPS獲取我的位置位置。我想從同一點獲得15次座標(以獲得最佳水平精度)。給任務之間的時間 - iOS

有沒有辦法在一個座標和另一個座標之間等待2秒?

我使用一個稱爲座標的對象,以緯度和經度爲屬性。

.... Exemple code 
Coordinate * coord = [[Coordinate alloc] init]; 
NSMutableArray *coordinates = [[NSMutableArray alloc] init]; 

for (i=0 ; i<=14; i++) 
{ 
    coord = newlocation; 
    [coordinates addObject:coord]; 
    .... some code to wait 2 seconds before add a new object to the array.... 
} 

我試圖使用NSThread sleepfortimeinterval,但視圖凍結。

感謝

回答

1

不是有for循環這樣,理論上可以使用重複NSTimer即每兩秒鐘發射,然後invalidate計時器後15次迭代。

但我不會建議這樣做,而是轉而採用事件驅動模式,等待撥打didUpdateLocations。如果didUpdateLocations尚未更新,則在兩秒內檢查沒有意義。同樣,在30秒內重複檢查15次也沒有意義,例如,5秒後您是否獲得了非常準確的位置。

我建議開始監控的位置,觀看的位置,因爲他們進來,隨後調用didUpdateLocations,並檢查CLLocationhorizontalAccuracy(它告訴你位置的精確程度)。一旦你到達期望的horizontalAccuracy,你可以聲明成功(例如停止監控位置或其他)。如果您願意,也可以建立一個NSTimer,在30秒後自動關閉位置監控。


例如:

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    NSLog(@"%s", __PRETTY_FUNCTION__); 

    [self startStandardUpdates]; 

    // after 30 seconds, if we haven't found a location, declare success with whatever we got (if anything) 

    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(30.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ 
     [self stopStandardUpdates]; // stop monitoring location if you want 

     if (!self.foundLocation) { 
      if (self.bestLocation) { 
       NSLog(@"Didn't find perfect location, but location has accuracy of %.1f meters", self.bestLocation.horizontalAccuracy); 
      } else { 
       NSLog(@"Even after 30 seconds, did not find any locations!"); 
      } 
     } 
    }); 
} 

#pragma mark - Location Services 

- (void)startStandardUpdates 
{ 
    // Create the location manager if this object does not 
    // already have one. 
    if (nil == self.locationManager) 
     self.locationManager = [[CLLocationManager alloc] init]; 

    self.locationManager.delegate = self; 
    self.locationManager.desiredAccuracy = kCLLocationAccuracyBest; 

    // Set a movement threshold for new events. 
    self.locationManager.distanceFilter = 5; 

    [self.locationManager startUpdatingLocation]; 
} 

- (void)stopStandardUpdates 
{ 
    [self.locationManager stopUpdatingLocation]; 
    self.locationManager = nil; 
} 

#pragma mark - CLLocationManagerDelegate 

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations 
{ 
    CLLocation* location = [locations lastObject]; 

    NSLog(@"%s: horizontalAccuracy = %.1f", __FUNCTION__, location.horizontalAccuracy); 

    if (location.horizontalAccuracy < 0) // not a valid location 
     return; 

    // this checks to see if the location is more accurate than the last; 
    // or you might just want to eliminate this `if` clause, because if 
    // you get updated location, you can probably assume it's better than 
    // the last one (esp if the user might be moving) 

    if (!self.bestLocation || location.horizontalAccuracy <= self.bestLocation.horizontalAccuracy) { 
     self.bestLocation = location; 
    } 

    if (location.horizontalAccuracy <= 5) { // use whatever you want here 
     NSLog(@"Found location %@", location); 
     self.foundLocation = YES; 
     [self stopStandardUpdates]; // stop it if you want 
    } 
} 

它使用以下屬性:

@property (nonatomic, strong) CLLocationManager *locationManager; 
@property (nonatomic, strong) CLLocation *bestLocation; 
@property (nonatomic) BOOL foundLocation; 
+0

在我的測試中,站在同一個地方,如果我兩次單擊按鈕來獲得位置(secods在觸摸之間),「水平精度」和「位置」由於信號振盪而改變。如果我只是停止監控位置,應用程序將需要很長時間才能再次建立良好的信號。 –

+0

@FelipeBoszczowski好的話,不要關閉位置服務。但是,這個想法是保持監測地點,直到(a)你獲得所需的準確性;或(b)已經過了一段時間。但是,不要只是迭代,保持檢查位置,因爲你可能已經找到了一個好的位置,如果沒有,在固定的時間內再次檢查沒有意義,因爲在你獲得之前沒有必要檢查另一個位置打電話給你的'didUpdateLocations'。請參閱我的修訂答案中的代碼示例作爲建議。按照您認爲合適的方式進行更改,但這是事件驅動方法的示例。 – Rob