如何在ios 8中获取当前位置lat? [英] how to get current location lat long in ios 8?

查看:159
本文介绍了如何在ios 8中获取当前位置lat?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

my issue is not get current location latitude and longitude in ios 8. i tried to set key in .plist file for ios 8 but but not call this method- didUpdateToLocation: please help this on issue.

我的代码是: -

- (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];
}

}

推荐答案

#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中添加
a密钥,并从
经理要求启动的位置请求授权。新的
位置授权有两个Info.plist键。需要这些键中的一个或两个。如果
这两个键都没有,你可以调用startUpdatingLocation但
位置管理器实际上不会启动。它不会向委托发送一个失败的
消息(因为它从未启动过,它不能
失败)。如果您添加一个或两个键但忘记
来明确请求授权,它也将失败。所以你需要做的第一件事是
是将以下一个或两个键添加到Info.plist文件中:

In iOS 8 you need to do two extra things to get location working: Add a key to your Info.plist and request authorization from the location manager asking it to start. There are two Info.plist keys for the new location authorization. One or both of these keys is required. If neither of the keys are there, you can call startUpdatingLocation but the location manager won’t actually start. It won’t send a failure message to the delegate either (since it never started, it can’t fail). It will also fail if you add one or both of the keys but forget to explicitly request authorization. So the first thing you need to do is to add one or both of the following keys to your Info.plist file:




  • NSLocationWhenInUseUsageDescription

  • NSLocationAlwaysUsageDescription

  • 这两个键都需要字符串


    这是您需要位置服务的原因说明。您可以在
    输入一个字符串,例如需要位置来查找您的位置
    ,与iOS 7一样,可以在InfoPlist.strings文件中进行本地化。

    which is a description of why you need location services. You can enter a string like "Location is required to find out where you are" which, as in iOS 7, can be localized in the InfoPlist.strings file.

    这篇关于如何在ios 8中获取当前位置lat?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆