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

iOS Get GPS Coordinates When No cellular Data

我需要能够在我的 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 的调用失败。您应该设置断点并跟踪代码以查看发生了什么。

Apple 文档说 https://developer.apple.com/reference/corelocation/clgeocoder

A geocoder object is a single-shot object that works with a network-based service to look up placemark information for its specified coordinate value.