LocationManager不一致

时间:2014-04-29 02:25:32

标签: ios objective-c gps core-location

我使用核心位置来存储纬度/经度坐标。我在viewDidLoad中有以下代码。

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

这是我的委托方法。

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
    [self.locationManager stopUpdatingLocation];
    CLLocation* loc = [locations lastObject]; // locations is guaranteed to have at least one object
    float latitude = loc.coordinate.latitude;
    float longitude = loc.coordinate.longitude;
    self.lat = [NSString stringWithFormat:@"%.8f",latitude];
    self.longString = [NSString stringWithFormat:@"%.8f",longitude];
}

我正在使用GPS跟踪这些坐标,似乎不一致。启动应用程序后,位置服务图标不会经常显示。但是,如果我切换我的wifi,它会工作。或者,如果我打开地图应用并提示它获取我当前的位置,然后切换到我的应用程序,它将工作。有时,它只会无所事事地工作。如果位置服务图标未显示,则我的lat / long值为null。有任何想法吗?提前谢谢!

1 个答案:

答案 0 :(得分:2)

问题是您在第一次更新到达时立即停止更新位置。这就像打开和关闭灯开关一样快,以至于灯泡没有时间开始发光。所有传感器需要时间和几次更新才能预热并获得适当的修复,尤其是GPS。您需要允许足够的更新到达,直到您确实有一个位置(并且您可能想要检查准确性以确保它是一个相当准确的位置)。