码迷,mamicode.com
首页 > 其他好文 > 详细

如何实现定位.

时间:2016-08-19 20:44:15      阅读:156      评论:0      收藏:0      [点我收藏+]

标签:

 1. 授权

 2. 开始更新用户位置

 2.1 代理回调方法处理数据

 3. 错误处理情况

  从iOS6开始, 要得到授权 (会弹出窗口, 用到了相关功能会自动)

    从iOS8开始, 要得到授权, 需要主动的调用代码来请求授权

 

#import "ViewController.h"

#import <CoreLocation/CoreLocation.h>

 @interface ViewController () <CLLocationManagerDelegate>

 授权, 定位相关

@property (strong, nonatomic) CLLocationManager *manager;

 @end

@implementation ViewController

- (void)viewDidLoad {

    [super viewDidLoad];

     // --------1. 授权 (Authorization) --------

    // 1. 判断当前的授权状态

    // 如果还没有做出决定, 要请求授权 (通常只会执行)

    if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusNotDetermined) {

        // 请求授权, 后台也可用

        // 注意: 要配置Info.plist文件

   ///////由于网络问题,plist文件配置图片上传不了,后续会补上??

        // 注意: 这个Key值, 一定不能有空格!

        [self.manager requestAlwaysAuthorization];

    }

     // --------2. 定位 (Location) --------

      // 开始更新用户位置的数据, 通过代理回调数据

    [self.manager startUpdatingLocation];

}

 

#pragma mark - CLLocationManagerDelegate

 // 当授权状态改变后触发

- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status

{

    // -------- 根据状态作出操作 --------

    // 1. 授权成功处理

    if (status == kCLAuthorizationStatusAuthorizedAlways) {

        NSLog(@"授权成功");

    }

        // 2. 授权失败处理, 提示用户.....

}

 //- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation

// 当位置信息更新后触发

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations

{

     // locations包含位置信息, 至少有一个location, 表示的是当前位置

     // 如果是延迟模式, 有多个值同时到达, 那么该数组有多个值

     // 当前的最新位置, 是数组的最后一个元素

   // CLLocation 表示位置(经纬度-地理坐标)的相关信息, 带有时间, 精确度等信息

    //    NSLog(@"%@", locations);

    // 最前的位置信息是数组的最后一个元素

    CLLocation *currentLocation = locations.lastObject;

    // CLLocationCoordinate2D 表示经纬度的结构体, 单位是Double

    CLLocationCoordinate2D coordinate = currentLocation.coordinate;

    // 获取到当前位置是的时间

    NSDate *date = currentLocation.timestamp;

      NSLog(@"%f, %f, %@", coordinate.latitude, coordinate.longitude, date);

}

 

// 遇到错误情况, 失败时触发

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error

{

    // 错误信息定义在CLError.h文件中

    NSLog(@"%@", error);

    

    switch (error.code) {

        case kCLErrorDenied: {

            NSLog(@"因为没有得到授权");

        }

        case kCLErrorNetwork: {

            NSLog(@"因为网络问题导致的错误");

        }

        case kCLErrorLocationUnknown: {

            NSLog(@"当前的位置未知, 会继续尝试");

       

             * 如果是模拟器, 报了kCLErrorLocationUnknown错误, 是模拟器的问题. 

             此时不是代码问题

             1. 删掉App重新运行

             2. 换一个模拟器

             3. 重置模拟器

        }

            break;

        default:

            break;

    }

}

#pragma mark - Getter & Setter

 - (CLLocationManager *)manager

{

    if (_manager == nil) {

        _manager = [CLLocationManager new];

        // 通过代理回调数据

        _manager.delegate = self;

    }

    return _manager;

}

 

@end

 

如何实现定位.

标签:

原文地址:http://www.cnblogs.com/yingge0121/p/5788787.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!