2014-06-26 38 views
1

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

//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

那么你保持位置更新在后台运行?你想在记录状态前几分钟开始更新? – Wain

+0

不,一旦按下UIButton,更新立即开始,并在达到stopTrying方法时停止。 – user2402616

+1

所以你最多允许10秒钟锁定一个位置 - 这样做不够... – Wain

回答

1

尝试设置此pausesLocationUpdatesAutomatically = NO与(当用户想要记录的坐标按钮按压)您的CLLocationManager

看起来像stopTrying是从来没有打电话(我猜,因为新初始化timeelapsed),所以GPS自动暂停。

此外,如果你想有一个“超时”定时器这个方法,你应该在你IBAction开始NSTimer,并呼吁stopTryinginvalidate定时器里面了。