【发布时间】:2014-08-17 02:15:46
【问题描述】:
您好,我正在使用带有蜂窝网络的 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];
【问题讨论】:
-
那么您是否让位置更新在后台运行?您是否在要记录状态前几分钟开始更新?
-
不,更新在按下 UIButton 时立即开始,并在到达 stopTrying 方法时停止。
-
所以您最多允许 10 秒的时间来锁定一个位置 - 这还不够......
-
看起来... Pham 对 pausesLocationUpdatesAutomatically = NO 的建议似乎给了我准确的结果。但是,这只是在具有虚假位置的模拟器中。我得把 iPad 带到外面试试看。
-
我允许 20 秒而不是 10 秒,它在模拟器上完美运行。但是,它一直挂在真实设备上。
标签: objective-c ipad ios7 gps core-location