即使没有数据,我也需要能够在我的 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/