如何在ios 8中获取当前位置lat?

时间:2014-10-01 05:25:15

标签: objective-c ios8

我的问题是在ios 8中没有得到当前位置的纬度和经度。我试着在ios 8的.plist文件中设置密钥,但是没有调用这个方法 - didUpdateToLocation:请帮助解决这个问题。

我的代码是: -

- (void)viewDidLoad

{

[super viewDidLoad];
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;

if(IS_OS_8_OR_LATER) {
    //[self.locationManager requestWhenInUseAuthorization];
    [self.locationManager requestWhenInUseAuthorization];
    [self.locationManager startUpdatingLocation];
}

}

1 个答案:

答案 0 :(得分:35)

#define IS_OS_8_OR_LATER ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
-(void)viewDidLoad
{  
[super viewDidLoad];
self.locationManager = [[CLLocationManager alloc] init];

self.locationManager.delegate = self;
if(IS_OS_8_OR_LATER){
    NSUInteger code = [CLLocationManager authorizationStatus];
    if (code == kCLAuthorizationStatusNotDetermined && ([self.locationManager respondsToSelector:@selector(requestAlwaysAuthorization)] || [self.locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)])) {
        // choose one request according to your business.
        if([[NSBundle mainBundle] objectForInfoDictionaryKey:@"NSLocationAlwaysUsageDescription"]){
            [self.locationManager requestAlwaysAuthorization];
        } else if([[NSBundle mainBundle] objectForInfoDictionaryKey:@"NSLocationWhenInUseUsageDescription"]) {
            [self.locationManager  requestWhenInUseAuthorization];
        } else {
            NSLog(@"Info.plist does not contain NSLocationAlwaysUsageDescription or NSLocationWhenInUseUsageDescription");
        }
    }
}
[self.locationManager startUpdatingLocation];
}

#pragma mark - CLLocationManagerDelegate
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
    NSLog(@"didFailWithError: %@", error);
    UIAlertView *errorAlert = [[UIAlertView alloc]
                               initWithTitle:@"Error" message:@"Failed to Get Your Location" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [errorAlert show];
}

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
    NSLog(@"didUpdateToLocation: %@", newLocation);
    CLLocation *currentLocation = newLocation;

    if (currentLocation != nil) {
        longitudeLabel.text = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.longitude];
        latitudeLabel.text = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.latitude];
    }
}
  

在iOS 8中,您需要做两件额外的事情才能让位置正常工作:添加   Info.plist的一个键,并从该位置请求授权   经理要求它开始。新的有两个Info.plist键   位置授权。需要这些键中的一个或两个。如果   这两个键都没有,你可以调用startUpdatingLocation但是   位置管理员实际上不会启动。它不会发送失败   消息给代表(因为它从未开始,它不能   失败)。如果你添加一个或两个键但忘记它也会失败   明确请求授权。所以你需要做的第一件事   是将以下一个或两个键添加到Info.plist文件中:

  • NSLocationWhenInUseUsageDescription
  • NSLocationAlwaysUsageDescription

这两个键都带有一个字符串

  

描述了您需要位置服务的原因。您可以   输入一个字符串,如“需要位置才能找到你所在的位置”   这与iOS 7一样,可以在InfoPlist.strings文件中进行本地化。

enter image description here