这是我的代码:
if (!self.locationManager) {
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
self.locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters;
}
if([CLLocationManager locationServicesEnabled]){
[self findCurrentLocation];
}
-(void)findCurrentLocation {
if ([self.locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]) {
[self.locationManager performSelector:@selector(requestAlwaysAuthorization)];
}
[self.locationManager startUpdatingLocation];
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
self.isLocationEnabled=YES;
self.currentLocation = newLocation;
if (currentLocation!= nil) {
latitudeValue = [NSString stringWithFormat:@"%.8f",currentLocation.coordinate.latitude];
longitudeValue = [NSString stringWithFormat:@"%.8f",currentLocation.coordinate.longitude];
}
[self.locationManager stopUpdatingLocation];
}
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
if([error code] == kCLErrorDenied)
{
[locationManager stopUpdatingLocation];
}
self.isLocationEnabled=NO;
}
我没有收到任何错误,但是didUpdateToLocation从未被调用,它将进入didFailWithError()
我获得纬度和经度的空值。请让我知道如何获得价值。
答案 0 :(得分:0)
:
向Info.plist
添加密钥,并请求位置管理员授权启动。
NSLocationWhenInUseUsageDescription
NSLocationAlwaysUsageDescription
您需要请求相应位置方法的授权。
[self.locationManager requestWhenInUseAuthorization]
[self.locationManager requestAlwaysAuthorization]
请阅读此文档:http://nevan.net/2014/09/core-location-manager-changes-in-ios-8/