CoreLocation使用GPS进行更新

时间:2014-06-26 09:28:48

标签: objective-c ipad ios7 gps core-location

您好我正在开发带有蜂窝电话的iPad Mini第一代,需要使用其内部GPS来获取纬度/经度坐标。我将在船上并按下一个按钮,将坐标保存到iPad上的文本文件中(文档目录)。然后我将在10英里之外航行并再次点击按钮,将新坐标附加到文本文件中。我将这样做8次,并且应该有8对不同的坐标。但是,应用程序非常不一致,要么保持相同的坐标,要么根本不获取任何坐标。提醒我,我不使用wifi /蜂窝电话,只使用内部GPS(船上的GPS能够获取信号,因此不是硬件问题)。我的代码如下。提前谢谢!

//in viewDidLoad

    self.locman = [[CLLocationManager alloc] init];
    self.locman.delegate = self;
    self.locman.desiredAccuracy = kCLLocationAccuracyBest;
    self.locman.distanceFilter = kCLDistanceFilterNone;
    self.locman.desiredAccuracy = kCLLocationAccuracyHundredMeters;

//委托方法

#define REQ_ACC 10
#define REQ_TIME 10

-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
    CLLocation* loc = locations.lastObject;
    CLLocationAccuracy acc = loc.horizontalAccuracy;
    NSDate* time = loc.timestamp;
    CLLocationCoordinate2D coord = loc.coordinate;
    if (!self.startTime) {
        self.startTime = [NSDate date];
        return; // ignore first attempt
    }
    NSLog(@"%f", acc);
    NSTimeInterval elapsed = [time timeIntervalSinceDate:self.startTime];
    if (elapsed > REQ_TIME) {
        NSLog(@"%@", @"This is taking too long");
        [self stopTrying];
        return;
    }
    if (acc < 0 || acc > REQ_ACC) {
        return; // wait for the next one
    }
    // got it
    NSLog(@"You are at: %f %f", coord.latitude, coord.longitude);
    self.lat = [NSString stringWithFormat:@"%.8f",coord.latitude];
    self.longString = [NSString stringWithFormat:@"%.8f",coord.longitude];
    [self stopTrying];
}


-(void) stopTrying {
    [self.locman stopUpdatingLocation];
    self.startTime = nil;
    self.trying = NO;
}

//这是在IBAction中(当用户想要记录坐标时按下按钮)

[self.locman startUpdatingLocation];

1 个答案:

答案 0 :(得分:1)

尝试使用pausesLocationUpdatesAutomatically = NO

设置此CLLocationManager

看起来永远不会调用stopTrying(我猜,因为新初始化timeelapsed)所以GPS会自动暂停。

此外,如果您想要此方法的“超时”计时器,则应该在NSTimer中启动IBAction并在其中调用stopTryinginvalidate计时器太