您的位置:首页 > 其它

定位 - CoreLocation - 区域报警

2015-09-21 16:47 281 查看
#import "ViewController.h"

#import <CoreLocation/CoreLocation.h>

@interface ViewController ()<CLLocationManagerDelegate>

/** 定位管理者对象 ***/

@property (nonatomic, strong) CLLocationManager *mgr;

@property (nonatomic, strong) CLCircularRegion *region;

@end

@implementation ViewController

- (CLLocationManager *)mgr{

if (!_mgr) {

_mgr = [[CLLocationManager alloc] init];

}

return _mgr;

}

- (void)viewDidLoad {

[super viewDidLoad];

//1. 设置代理

self.mgr.delegate = self;

// 3. 开始监听用户所在区域

// CLRegion 有两个子类, 用来指定区域

// 指定蓝牙范围: CLBeaconRegion

// 指定圆形范围: CLCircularRegion

// 创建中心点

CLLocationCoordinate2D center = CLLocationCoordinate2DMake(39.915094,116.487775);

// 创建圆形区域, 指定中心点 经纬度以及半径

self.region = [[CLCircularRegion alloc] initWithCenter:center radius:1000 identifier:@"大望路"];

// [注意] 如果是ios8 必须主动请求用户 授权

if(IOS8){

[self.mgr requestAlwaysAuthorization];

}else{

[self.mgr startMonitoringForRegion:self.region];

}

}

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

{

if (status == kCLAuthorizationStatusNotDetermined) {

LogGreen(@"正在授权");

}else if ((status == kCLAuthorizationStatusAuthorizedAlways) || (status == kCLAuthorizationStatusAuthorizedWhenInUse)){

[self.mgr startMonitoringForRegion:self.region];

}else{

LogGreen(@"授权失败");

}

}

#pragma mark - CLLocationManagerDelegate

/**

* 进入某个监听区域 开始调用

*/

- (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region

{

LogRed(@" 进入某个监听区域 开始调用");

}

/**

* 离开监听区域开始调用

*/

- (void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region

{

LogRed(@" 超出某个监听区域 开始调用");

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: