无法通过 GPS 获取 ios8 中的经度和纬度

Unable to get longitude and latitude in ios8 by GPS

我正在使用 iOS8 并使用以下代码通过 GPS 获取经度和纬度:

-(void)CurrentLocationIdentifier
{
    locationManager = [CLLocationManager new];
    locationManager.delegate = self;
    locationManager.distanceFilter = kCLDistanceFilterNone;
    locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0 &&
    [CLLocationManager authorizationStatus] != kCLAuthorizationStatusAuthorizedWhenInUse)
    //[CLLocationManager authorizationStatus] != kCLAuthorizationStatusAuthorizedAlways)
    {
    // Will open an confirm dialog to get user's approval
    [locationManager requestWhenInUseAuthorization];
    //[_locationManager requestAlwaysAuthorization];
    } else {
    [locationManager startUpdatingLocation]; //Will update location immediately
    }
}

- (void)locationManager:(CLLocationManager *)manager
 didUpdateLocations:(NSArray *)locations {
    CLLocation *location = [locations lastObject];
    NSLog(@"lat%f - lon%f", location.coordinate.latitude,   location.coordinate.longitude);
 }

- (void)locationManager:(CLLocationManager*)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status
{
    switch (status) {
    case kCLAuthorizationStatusNotDetermined: {
        NSLog(@"User still thinking..");
    }
    case kCLAuthorizationStatusDenied: {
        NSLog(@"User hates you");
    } break;
    case kCLAuthorizationStatusAuthorizedWhenInUse:
    case kCLAuthorizationStatusAuthorizedAlways: {
        [locationManager startUpdatingLocation]; //Will update location immediately
    } break;
    default:
        break;
}
}

在 运行 这段代码之后,我在日志中得到的输出是:用户仍在思考。谁能帮我解决这个问题我是 iOS 和 CoreLocation.Framework 的新手? 请帮助我找到错误以及如何解决它。

在 iOS 8 中,您需要做两件额外的事情才能让位置正常工作:向您的 Info.plist 添加一个密钥,并向位置管理器请求授权以启动它。新位置授权有两个 Info.plist 密钥。这些键中的一个或两个是必需的。如果两个键都不存在,您可以调用 startUpdatingLocation 但位置管理器不会真正启动。它也不会向委托发送失败消息(因为它从未启动,所以它不会失败)。如果您添加一个或两个密钥但忘记明确请求授权,它也会失败。

因此,您需要做的第一件事是将以下一个或两个键添加到您的 Info.plist 文件中:

NSLocationWhenInUseUsageDescription
NSLocationAlwaysUsageDescription

这两个键都带有一个字符串,它描述了您需要定位服务的原因。您可以输入一个字符串,如“Location is required to find out where you are”,如 iOS 7,可以在 InfoPlist.strings 文件中本地化。

在 InfoPlist.strings 个文件中添加此字符串

1) NSLocationWhenInUseUsageDescription

2) NSLocationAlwaysUsageDescription

试试这个代码

locationManagerApp=[[CLLocationManager alloc] init];

    locationManagerApp.delegate = self;
    locationManagerApp.distanceFilter = kCLDistanceFilterNone;
    locationManagerApp.desiredAccuracy = kCLLocationAccuracyHundredMeters;

    if([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
    {        
           [locationManagerApp requestAlwaysAuthorization];
    }

    [locationManagerApp startUpdatingLocation];
    CLLocation *location1 = [locationManagerApp location];
    CLLocationCoordinate2D coordinate = [location1 coordinate];
    self.latValue= [NSString stringWithFormat:@"%f", coordinate.latitude];
    self.longValue = [NSString stringWithFormat:@"%f", coordinate.longitude];
    NSLog(@"Latitude  = %@", self.latValue);
    NSLog(@"Longitude = %@", self.longValue);

并且 运行 项目在设备中。