区域监听

  • 区域顾名思义是一个指定的区域,对其进行监听。先普及一下一点知识
经纬度的基本知识

前提:加载一张指南针图片,其他的#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
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 一、基本步骤 1.导入CoreLocation框架和对应的主头文件 #import <CoreLocation/C...
    iOS_成才录阅读 5,557评论 4 6
  • @interface ViewController() @property(nonatomic,strong)CL...
    流沙3333阅读 1,950评论 0 0
  • 区域监听 1.概念解释 区 域 : 就是指划定的一块地域范围(比如圆形区域, 则由区域中心, 和半径组成) ...
    翻这个墙阅读 2,885评论 0 0
  • CoreLocation框架 一. iOS8.0之前的定位(✨✨✨✨✨) 前台定位导入CoreLocation框架...
    尼古拉斯赵四爷阅读 4,695评论 0 2
  • 你会不会也在秋风起时感念时光。 苇丛嚷嚷声里每次都有风的到来,塘下的水波再也圈不住冒头的鱼,圆形排开涌向远处的岸,...
    好吧缄默阅读 2,047评论 0 0

友情链接更多精彩内容