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

是否有一种方法可以等待一个坐标与另一个坐标之间的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,但是视图冻结。

谢谢

最佳答案

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

但是我不建议这样做,而是转向事件驱动模型,等待对您的didUpdateLocations的调用。如果didUpdateLocations尚未更新,则在两秒钟内检查毫无意义。同样,例如,如果您在5秒后获得了一个准确的位置,那么在30秒内重复检查15次也没有意义。

我建议开始监视位置,在随后调用didUpdateLocations时观察它们的位置,并检查horizontalAccuracyCLLocation(它告诉您位置的准确性)。达到所需的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;

关于ios - 在任务之间留出时间-iOS,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22790700/

10-10 20:50