地理问题iOS 开发每天分享优质文章iOS开发

区域监听

2016-06-29  本文已影响123人  IIronMan
经纬度的基本知识

前提:加载一张指南针图片,其他的#import <CoreLocation/CoreLocation.h>框架都需要导入,以及定位管理对象,挂代理,遵守协议,利用对象来调用 startMonitoringForRegion:(nonnull CLRegion *),然后在对其设置

-下面是具体的代码

#import "ViewController.h"
#import <CoreLocation/CoreLocation.h>

@interface ViewController ()<CLLocationManagerDelegate>
@property(nonatomic,strong)CLLocationManager *locationManager;

@end

@implementation ViewController

//1.创建管理者对象  懒加载

-(CLLocationManager *)locationManager
{
  if (!_locationManager) {
    
    _locationManager = [[CLLocationManager alloc]init];
}

return _locationManager;
}


- (void)viewDidLoad {
[super viewDidLoad];

//2.挂代理,遵守协议

self.locationManager.delegate = self;

//注意:如果是ios8,想进入区域检测,必须自己主动请求获取用户隐私的权限
if ([[UIDevice currentDevice].systemVersion doubleValue] >= 8.0) {
    
    //4.主动要求用户对我们的程序授权,授权状态改变就会通知代理status
    [self.locationManager requestAlwaysAuthorization];
    
}else
{
    NSLog(@"不是ios8");
}

//3.开始检测用户所在的区域(后面给一个区域)
//3.1.创建区域

//CLRegion有两个子类是专门用来指定区域的,一个可以指定蓝牙的范围,一个可以用来指定圆形的范围
//创建我们的中心点
CLLocationCoordinate2D center = CLLocationCoordinate2DMake(40.058501, 116.304171);

CLCircularRegion *circular = [[CLCircularRegion alloc]initWithCenter:center radius:500 identifier:@"软件园"];

[self.locationManager startMonitoringForRegion:circular];

}

 #pragma mark -CLLocationMangerDelegate  代理方法的调用

//进入一个区域
-(void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region
{

NSLog(@"进入监听区域");

}
//离开区域时调用
-(void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region
{

NSLog(@"离开监听区域");

}

@end
上一篇 下一篇

猜你喜欢

热点阅读