标签:
#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(@" 超出某个监听区域 开始调用");
}
标签:
原文地址:http://www.cnblogs.com/guangleijia/p/4826609.html