在任务之间给出时间 - iOS

时间:2014-04-01 15:46:07

标签: ios objective-c gps core-location wait

我从iPhone的GPS获取我的位置位置。我希望从同一点获得坐标15次(以获得最佳水平精度)。

有没有办法等待一个坐标和另一个坐标之间的2秒?

我使用一个名为coordinate的对象,纬度和经度作为属性。

.... 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 个答案:

答案 0 :(得分:1)

理论上,您可以使用每两秒触发一次的重复for,然后在15次迭代后NSTimer计时器,而不是像这样的invalidate循环。

但我不建议这样做,而是转移到事件驱动的模型,等待对didUpdateLocations的调用。如果didUpdateLocations尚未更新,则在两秒内检查是没有意义的。同样,例如,如果您在5秒后获得非常准确的位置,则在30秒内反复检查15次是没有意义的。

我建议您开始监控位置,在后续调用didUpdateLocations时查看位置,并检查horizontalAccuracy的{​​{1}}(告诉您有多准确)位置是)。达到所需的CLLocation后,您可以宣布成功(例如停止监控位置或其他)。您还可以建立一个horizontalAccuracy,如果您愿意,还可以在30秒后自动关闭对位置的监控。


例如:

NSTimer

这使用以下属性:

- (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
    }
}