我试图获得经度&用户当前位置的纬度,我的代码就是这样:
- (void)viewDidLoad {
self.locationManager=[[CLLocationManager alloc]init];
[locationManager setDelegate:self];
[locationManager setDesiredAccuracy:kCLLocationAccuracyNearestTenMeters];
[locationManager startUpdatingLocation];
}
-(void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
{
float latitude=newLocation.coordinate.latitude;
float longitude=newLocation.coordinate.longitude;
NSLog(@"%f",latitude);
NSLog(@"%f",longitude);
[locationManager stopUpdatingLocation];
}
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
NSLog(@"didFailWithError: %@", error);
}
我总是从didFailWithError方法得到这个错误:
didFailWithError: Error Domain=kCLErrorDomain Code=0 "The operation couldn’t be completed. (kCLErrorDomain error 0.)"
答案 0 :(得分:2)
.plist
中添加以下权限
NSLocationWhenInUseUsageDescription
,该值为Application would like to use your location
,其次是,在代码中添加[locationManager requestWhenInUseAuthorization];
行。
我添加了这段代码:
- (void)viewDidLoad {
self.locationManager=[[CLLocationManager alloc]init];
[locationManager setDelegate:self];
[locationManager setDesiredAccuracy:kCLLocationAccuracyNearestTenMeters];
[locationManager requestWhenInUseAuthorization];
[locationManager startUpdatingLocation];
}