iOS 定位功能的实现详解

来源:互联网 发布:extend软件 编辑:程序博客网 时间:2024/04/29 01:09
  1. 导入框架

     Xcode中添加“CoreLocation.framework
  2. 导入主头文件

     #import <CoreLocation/CoreLocation.h>
  3. 声明管理器和代理

     @interface ViewController ()<CLLocationManagerDelegate> @property (nonatomic, strong) CLLocationManager* locationManager; @end
  4. 初始化管理器

     self.locationManager = [[CLLocationManager alloc] init]; self.locationManager.delegate = self; self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
  5. 开启定位服务,需要定位时调用findMe方法:

     - (void)findMe {     /** 由于IOS8中定位的授权机制改变 需要进行手动授权      * 获取授权认证,两个方法:      * [self.locationManager requestWhenInUseAuthorization];      * [self.locationManager requestAlwaysAuthorization];      */     if ([self.locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]) {         NSLog(@"requestAlwaysAuthorization");         [self.locationManager requestAlwaysAuthorization];     }     //开始定位,不断调用其代理方法     [self.locationManager startUpdatingLocation];     NSLog(@"start gps"); }
  1. 代理设置

     - (void)locationManager:(CLLocationManager *)manager      didUpdateLocations:(NSArray *)locations {     // 1.获取用户位置的对象     CLLocation *location = [locations lastObject];     CLLocationCoordinate2D coordinate = location.coordinate;     NSLog(@"纬度:%f 经度:%f", coordinate.latitude, coordinate.longitude);     // 2.停止定位     [manager stopUpdatingLocation]; } - (void)locationManager:(CLLocationManager *)manager        didFailWithError:(NSError *)error {     if (error.code == kCLErrorDenied) {         // 提示用户出错原因,可按住Option键点击 KCLErrorDenied的查看更多出错信息,可打印error.code值查找原因所在     } }

注意事项

  1. iOS8中定位服务的变化:CLLocationManager协议方法不响应,无法回掉GPS方法,不出现获取权限提示,解决方法如下,详见1 详见2

     如果需要仅在前台定位,你在调用startUpdatingLocation 前需要调用requestWhenInUseAuthorization(见设置步骤6)   如果需要在前后台定位,你在调用startUpdatingLocation 前需要调用requestAlwaysAuthorization   在plist文件中添加NSLocationWhenInUseUsageDescription或(与)NSLocationAlwaysUsageDescription字段:   找到info.plist文件->右击->Open As->Source Code->在尾部的</dict>标签之前添加以下一个或两个:   <key>NSLocationWhenInUseUsageDescription</key><string>需要定位</string>   <key>NSLocationAlwaysUsageDescription</key><string>需要定位</string>
  2. 用户隐私的保护

     从iOS 6开始,苹果在保护用户隐私方面做了很大的加强,以下操作都必须经过用户批准授权:要想获得用户的位置,想访问用户的通讯录、日历、相机、相册等;当想访问用户的隐私信息时,系统会自动弹出一个对话框让用户授权。 可以在Info.plist中设置NSLocationUsageDescription说明定位的目的(Privacy - Location Usage Description) 从iOS 8开始,用户定位分两种情况   总是使用用户位置:NSLocationAlwaysUsageDescription   使用应用时定位:NSLocationWhenInUseDescription   当想访问用户的隐私信息时,系统会自动弹出一个对话框让用户授权 /**  *打开定位服务  *需要在info.plist文件中添加(以下二选一,两个都添加默认使用NSLocationWhenInUseUsageDescription):  *NSLocationWhenInUseUsageDescription 允许在前台使用时获取GPS的描述  *NSLocationAlwaysUsageDescription 允许永远可获取GPS的描述
  3. 获取当前软件的定位服务状态

     if ([CLLocationManager locationServicesEnabled]  //确定用户的位置服务启用   &&[CLLocationManager authorizationStatus]==kCLAuthorizationStatusDenied)   //位置服务是在设置中禁用       {       }
  4. 代码:调用startLocation方法即可

    #pragma mark Location and Delegate    - (void)startLocation    {        self.locationManager = [[CLLocationManager alloc] init];        self.locationManager.delegate = self;        self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;        /** 由于IOS8中定位的授权机制改变 需要进行手动授权         * 获取授权认证,两个方法:         * [self.locationManager requestWhenInUseAuthorization];         * [self.locationManager requestAlwaysAuthorization];         */        if ([self.locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {            NSLog(@"requestWhenInUseAuthorization");            //        [self.locationManager requestWhenInUseAuthorization];            [self.locationManager requestAlwaysAuthorization];        }        //开始定位,不断调用其代理方法        [self.locationManager startUpdatingLocation];        NSLog(@"start gps");    }    - (void)locationManager:(CLLocationManager *)manager         didUpdateLocations:(NSArray *)locations    {        // 1.获取用户位置的对象        CLLocation *location = [locations lastObject];        CLLocationCoordinate2D coordinate = location.coordinate;        NSLog(@"纬度:%f 经度:%f", coordinate.latitude, coordinate.longitude);        self.longitute = [NSNumber numberWithDouble:coordinate.longitude];        self.latitude = [NSNumber numberWithDouble:coordinate.latitude];        // 2.停止定位        [manager stopUpdatingLocation];    }    - (void)locationManager:(CLLocationManager *)manager           didFailWithError:(NSError *)error    {        if (error.code == kCLErrorDenied) {            // 提示用户出错原因,可按住Option键点击 KCLErrorDenied的查看更多出错信息,可打印error.code值查找原因所在        }    }
0 0