objective-c开发——地图定位之获取位置信息

 

在OC中,开发地图定位功能主要用到两个框架

1、CLLocation

2、MapKit

今天我们先了解一下CLLocation中的一些基本操作。

我们编写代码边说。

你知道,无论是JAVA 还是OC,我们要用到某个系统类总有 XXManager的类

比如:android中的fragmentManager

         OC中的FileManager

这次,我们用到的CLLocation中也有类似的类——CLLocationManager。

 

//

//  ViewController.m

//  Demo1-获取位置信息

//

//  Created by tarena on 16/3/21.

//  Copyright © 2016 tarena. All rights reserved.

//

 

#import "ViewController.h"

#import <CoreLocation/CoreLocation.h>

 

@interface ViewController ()<CLLocationManagerDelegate>

/** 管理器 **/

//^_^这个东西,是CLLocation的一个管理类

@property (nonatomic, strong)CLLocationManager *mgr;

@end

 @implementation ViewController

 

- (CLLocationManager *)mgr{

    if (!_mgr) {

        // 1.创建定位管理器

        self.mgr = [[CLLocationManager alloc]init];        

        // 2.设置代理

        self.mgr.delegate = self;

        

        // 3.询问用户是否允许开启定位(前台定位)

        //WhenInUse:默认只支持前台定位

        //但:可以通过两步设置,是的whenInUser也能后台定位

        //step1:target中勾选后台定位选项

        //step2:设置managerallowsBackgroundyes

        //always进入后台后的区别,在于whenInUse模式会在

        //顶部出现蓝色提示条

        //[self.mgr requestWhenInUseAuthorization];

        //self.mgr.allowsBackgroundLocationUpdates = YES;

        

        //Always:默认支持前后台定位

        [self.mgr requestAlwaysAuthorization];

    }

    return _mgr;

}

 

- (void)viewDidLoad {

    [super viewDidLoad];

}

 

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {

    // 1.设备是否开启了定位服务

    if (![CLLocationManager locationServicesEnabled])

    {

        NSLog(@"定位服务不可用,请打开定位功能");

        return;

    }

    

    // 2.检测本应用的定位服务是否已经开启

    if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusDenied) {

        NSLog(@"你已经拒绝了开启定位,还是去设置中打开吧");

        return;

    }

    

    // 3.开始更新位置信息(定位)

    //[self.mgr startUpdatingLocation];

    

    // requestLocationiOS9之后才有的

    // 特点:只会发一次请求定位的消息

    // 注意:要想使用此方法,必须在代理中添加locationManager:didFailWithError:方法

    // 否则系统崩溃

    [self.mgr requestLocation];

    

 

}

 

 

#pragma mark - CLLocationManagerDelgate

/**

 *  定位的主方法

 *

 *  @param manager   <#manager description#>

 *  @param locations <#locations description#>

 */

- (void)locationManager:(CLLocationManager *)manager

     didUpdateLocations:(NSArray<CLLocation *> *)locations

{

    NSLog(@"定位中....");

    // 如果已经获取到了定位信息,则无需再多次更新位置信息

    //[self.mgr stopUpdatingLocation];

}

 

/**

 *  当使用管理器的requestLocation方法时,一定要添加

 *  此方法,否则系统崩溃

 *

 */

- (void)locationManager:(CLLocationManager *)manager

       didFailWithError:(NSError *)error

{

    NSLog(@"定位失败");

} 

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

{

    //这里,是对当前用户对定位是否授权的一个状态的判断

    switch (status) {

        case kCLAuthorizationStatusNotDetermined:

            NSLog(@"用户还没做出决定");

            break;

        case kCLAuthorizationStatusRestricted:

            NSLog(@"访问受限");

            break;

        case kCLAuthorizationStatusDenied:

            NSLog(@"用户选择了不允许");

            break;

        case kCLAuthorizationStatusAuthorizedAlways:

            NSLog(@"开启了Alway模式");

            break;

        case kCLAuthorizationStatusAuthorizedWhenInUse:

            NSLog(@"开启了whenInUse状态");

            break;

        default:

            NSLog(@"default");

            break;

    }

}

@end

请不要忘记,我们需要在InfoPlist中添加授权提示

 

 

 

posted @ 2016-03-23 22:01  师然  阅读(534)  评论(0编辑  收藏  举报