iOS CLLocationManager定位,IOS8注意

来源:互联网 发布:折八百淘宝商城下载 编辑:程序博客网 时间:2024/05/24 00:14

今天下午动手用了IOS自带的定位,结果在网上看了很多教程,也将示例代码直接运行,但就是一直无法获取位置,代码如下:

首先导入CoreLocation.framework,然后再引入头文件#import <CoreLocation/CoreLocation.h>

定义属性

@property (nonatomic , strong)CLLocationManager *locationManager;
然后使用代理 CLLocationManagerDelegate

- (void)locate{    // 判断定位操作是否被允许    if([CLLocationManager locationServicesEnabled]) {        //定位初始化        _locationManager=[[CLLocationManager alloc] init];        _locationManager.delegate=self;        _locationManager.desiredAccuracy=kCLLocationAccuracyBest;        _locationManager.distanceFilter=10;        [_locationManager startUpdatingLocation];//开启定位    }else {        //提示用户无法进行定位操作        UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"提示" message:@"定位不成功 ,请确认开启定位" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];        [alertView show];    }    // 开始定位    [_locationManager startUpdatingLocation];}#pragma mark - CoreLocation Delegate-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{    //此处locations存储了持续更新的位置坐标值,取最后一个值为最新位置,如果不想让其持续更新位置,则在此方法中获取到一个值之后让locationManager stopUpdatingLocation    CLLocation *currentLocation = [locations lastObject];    // 获取当前所在的城市名    CLGeocoder *geocoder = [[CLGeocoder alloc] init];    //根据经纬度反向地理编译出地址信息    [geocoder reverseGeocodeLocation:currentLocation completionHandler:^(NSArray *array, NSError *error)    {        if (array.count > 0)        {            CLPlacemark *placemark = [array objectAtIndex:0];            //NSLog(@"%@",placemark.name);//具体位置            //获取城市            NSString *city = placemark.locality;            if (!city) {                //四大直辖市的城市信息无法通过locality获得,只能通过获取省份的方法来获得(如果city为空,则可知为直辖市)               city = placemark.administrativeArea;            }            cityName = city;            NSLog(@"定位完成:%@",cityName);            //系统会一直更新数据,直到选择停止更新,因为我们只需要获得一次经纬度即可,所以获取之后就停止更新            [manager stopUpdatingLocation];         }else if (error == nil && [array count] == 0)         {            NSLog(@"No results were returned.");         }else if (error != nil)         {             NSLog(@"An error occurred = %@", error);         }    }];}

viewDidLoad中调用了locate之后一直没有数据返回,代理也不执行,在网上找了很多教程,代码大多大同小异,但是结果都是不理想,偶然之间看到ios8的定位和低版本的不同,我的手机刚好是ios8,便点开那个博客去看看,博客请点此进入

以下为该博客摘要:

在iOS8以前的版本中,我们使用CLLocationManager定位是没有问题的,最近在iOS8系统中却无法定位了。。。。这是一大问题啊!

iOS8中使用CoreLocation定位

1、在使用CoreLocation前需要调用如下函数【iOS8专用】:
iOS8对定位进行了一些修改,其中包括定位授权的方法,CLLocationManager增加了下面的两个方法:
(1)始终允许访问位置信息

- (void)requestAlwaysAuthorization;
(2)使用应用程序期间允许访问位置数据

- (void)requestWhenInUseAuthorization;
示例如下:
locationManager=[[CLLocationManager alloc] init];    locationManager.delegate=self;    locationManager.desiredAccuracy=kCLLocationAccuracyBest;    locationManager.distanceFilter=10;    if (iOSVersion>=8) {        [locationManager requestWhenInUseAuthorization];//使用程序其间允许访问位置数据(iOS8定位需要)    }    [locationManager startUpdatingLocation];//开启定位
2、在Info.plist文件中添加如下配置:
(1)NSLocationAlwaysUsageDescription
(2)NSLocationWhenInUseUsageDescription


这样添加后,定位功能就能正常使用了!


原文链接:http://www.cnblogs.com/boyuanmeng/p/4211680.html


0 0
原创粉丝点击