iOS 在没有蜂窝数据时获取 GPS 坐标

标签 ios gps cllocationmanager

即使没有数据,我也需要能够在我的 iOS 应用程序上获取 GPS 坐标。我认为这不是问题,因为 GPS 与蜂窝信号是分开的,但我对此有疑问。这是我到目前为止所拥有的。如果蜂窝数据关闭,我不会收到任何错误消息,它只是无法获取坐标并将这些值保留为空:

- (void)viewDidLoad {
    [super viewDidLoad];
    self.locationManager = [[CLLocationManager alloc] init];
    self.locationManager.delegate = self;
    self.locationManager.desiredAccuracy=kCLLocationAccuracyBest;
    self.locationManager.distanceFilter=kCLDistanceFilterNone;
    [self.locationManager requestWhenInUseAuthorization];
    [self.locationManager startUpdatingLocation];
    geocoder = [[CLGeocoder alloc] init];
}
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
    CLLocation *newLocation = [locations lastObject];
    [geocoder reverseGeocodeLocation:newLocation completionHandler:^(NSArray *placemarks, NSError *error) {
        if (error == nil&& [placemarks count] >0) {
            placemark = [placemarks lastObject];
            NSString *latitude, *longitude, *state, *country;
            latitude = [NSString stringWithFormat:@"%f",newLocation.coordinate.latitude];
            longitude = [NSString stringWithFormat:@"%f",newLocation.coordinate.longitude];
            self.theCoord.text = [[latitude stringByAppendingString:@", "] stringByAppendingString:longitude];

        } else {
            NSLog(@"%@", error.debugDescription);
        }
    }];
    // Turn off the location manager to save power.
    [self.locationManager stopUpdatingLocation];
}

最佳答案

位置更新应该在没有互联网的情况下工作,但地理编码不会。那是一个网络服务。我的猜测是您正在获取位置更新,但对 reverseGeocodeLocation 的调用失败。您应该设置断点并跟踪代码以查看发生了什么。

关于iOS 在没有蜂窝数据时获取 GPS 坐标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39583806/

相关文章:

ios - 我的应用程序没有出现在 iOS6 的位置设置列表中

iOS:cocoa Pods:如何向现有 pod 添加新的依赖项?

ios - 应用程序加载程序 : Apple's web service operation was not successful

ios - 在 iOS 6 中将 5112225.14 转换为 5,112,225.14

android - 不调用 GPS 更新

ios6 - iOS 6 中的“pausesLocationUpdatesAutomatically”未按预期工作

ios - 位置服务权限警报消失

ios - 在 Xcode iOS 模拟器上获取旧操作系统版本

ios - 我可以在不在 iphone 中生成默认弹出窗口的情况下获取位置访问权限吗?

gps - gpsd 使用什么格式进行馈送?