没有手机数据时iOS获取GPS坐标

时间:2016-09-20 00:05:38

标签: 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];
}

2 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:0)

Apple doc说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.

相关问题