【发布时间】:2016-07-23 18:56:10
【问题描述】:
我尝试获取用户当前位置的经度和纬度,我的代码就是这样:
- (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.)"
【问题讨论】:
-
我重置了网络,但仍然无法正常工作。 @derpoliuk
-
@Mr.Bond 请检查您在模拟器中的位置 Debug>Location>Current Location
-
那也行不通。@bhavin ramani
-
@Mr.Bond 你是在模拟器还是在xcode中设置了你的当前位置?
-
您是否在项目的 .plist 中添加 NSLocationWhenInUseUsageDescription 这个键?如果您忘记了,请在 .plist 中添加此键和值 NSLocationWhenInUseUsageDescription = 应用程序想使用您的位置
标签: ios core-location cllocationmanager