CLRegion监听区域
来源:互联网 发布:纵横九州神羽进阶数据 编辑:程序博客网 时间:2024/06/01 10:16
如果希望iOS设备进入某个区域发出通知,那么这种区域监测的功能也被称为临近警告。所谓临近警告的示意图如图9.6所示。
用户设备不断地临近指定固定点,当与该固定点的距离小于指定范围时,系统可以触发相应的处理。用户设备离开指定固定点,当与该固定点的距离大于指定范围时,系统也可以触发相应的处理。
CLLocationManagerDelegate>
@property (strong,nonatomic) CLLocationManager *locationManager;
- (void)viewDidLoad
{
[super viewDidLoad];
if([CLLocationManager locationServicesEnabled])//判断定位功能是否开启
{
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
// 定义一个CLLocationCoordinate2D作为区域的圆心
CLLocationCoordinate2D companyCenter;
companyCenter.latitude = 22;
companyCenter.longitude = 11;
// 使用CLCircularRegion创建一个圆形区域,半径为50米
不要使用CLRegionCLRegion *clr = [[CLCircularRegion alloc] initWithCenter:companyCenter
radius:50 identifier:@"car"];
// 开始监听fkit区域
[self.locationManager startMonitoringForRegion:clr];
}
else
{
//提醒用户开启定位功能
[[[UIAlertView alloc] initWithTitle:@"当前无定位功能"
message:@"开启定位" delegate:self
cancelButtonTitle:@"确定" otherButtonTitles: nil] show];
}
}
// 进入指定区域以后将弹出提示框
-(void)locationManager:(CLLocationManager *)manager
didEnterRegion:(CLRegion *)region
{
[[[UIAlertView alloc] initWithTitle:@"请注意"
message:@"您现在的位置是" delegate:nil
cancelButtonTitle:@"OK" otherButtonTitles:nil] show];
}
// 离开指定区域以后将弹出提示框
-(void)locationManager:(CLLocationManager *)manager
didExitRegion:(CLRegion *)region
{
[[[UIAlertView alloc] initWithTitle:@"请注意"
message:@"您现在的位置是。。。。。。" delegate:nil
cancelButtonTitle:@"OK" otherButtonTitles:nil] show];
}
开程序员的淘宝店!寻找开源技术服务伙伴!>>> »
9.4 区域监测
如果希望iOS设备进入某个区域发出通知,那么这种区域监测的功能也被称为临近警告。所谓临近警告的示意图如图9.6所示。
图9.6 临近警告的示意图
用户设备不断地临近指定固定点,当与该固定点的距离小于指定范围时,系统可以触发相应的处理。用户设备离开指定固定点,当与该固定点的距离大于指定范围时,系统也可以触发相应的处理。
iOS的区域监测同样可以使用CLLocationManager来实现,监听设备是否进入/离开某个区域的步骤如下。
创建CLLocationManager对象,该对象负责获取定位相关信息,并为该对象设置一些必要的属性。对于区域监测而言,CLLocationManager对象需要设置monitoredRegions属性,该属性值用于设置该设备监听的多个区域。
为CLLocationManager指定delegate属性,该属性值必须是一个实现CLLocationManagerDelegate协议的对象。实现CLLocationManagerDelegate协议时可根据需要实现协议中特定的方法。
调用CLLocationManager的startMonitoringForRegion:方法进行区域监测。区域监测结束时,可调用stopMonitoringForRegion:方法结束区域监测。
当设备进入指定区域时,iOS系统将会自动激发CLLocationManager的delegate对象的locationManager:didEnterRegion:方法;当设备离开指定区域时,iOS系统将会自动激发CLLocationManager的delegate对象的locationManager:didExitRegion:方法,开发者可重写这两个方法对用户进行提醒。
iOS提供了CLRegion来定义被监测的区域,但实际编程中推荐使用CLCircularRegion(CLRegion的子类)创建圆形区域,创建CLCircularRegion对象时无非就是指定圆心、半径等信息,非常简单。下面示例会进行详细示范。
新建一个Single View Application,该应用无须修改界面设计文件,直接修改视图控制器类的实现部分来监测设备是否进入、离开某个区域。该示例的视图控制器类的实现部分代码如下。
程序清单:codes/09/9.4/RegionMonitor/RegionMonitor/FKViewController.m
@interface FKViewController () <CLLocationManagerDelegate>
@property (retain,nonatomic) CLLocationManager *locationManager;
@end
@implementation FKViewController
- (void)viewDidLoad
{
[super viewDidLoad];
if([CLLocationManager locationServicesEnabled])
{
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
// 定义一个CLLocationCoordinate2D作为区域的圆心
CLLocationCoordinate2D companyCenter;
companyCenter.latitude = 23.126272;
companyCenter.longitude = 113.395568;
// 使用CLCircularRegion创建一个圆形区域,半径为500米
CLRegion* fkit = [[CLCircularRegion alloc] initWithCenter:companyCenter
radius:500 identifier:@"fkit"];
// 开始监听fkit区域
[self.locationManager startMonitoringForRegion:fkit];
}
else
{
// 使用警告框提醒用户
[[[UIAlertView alloc] initWithTitle:@"提醒"
message:@"您的设备不支持定位" delegate:self
cancelButtonTitle:@"确定" otherButtonTitles: nil] show];
}
}
// 进入指定区域以后将弹出提示框提示用户
-(void)locationManager:(CLLocationManager *)manager
didEnterRegion:(CLRegion *)region
{
[[[UIAlertView alloc] initWithTitle:@"区域检测提示"
message:@"您已经【进入】疯狂软件教育中心区域内!" delegate:nil
cancelButtonTitle:@"OK" otherButtonTitles:nil] show];
}
// 离开指定区域以后将弹出提示框提示用户
-(void)locationManager:(CLLocationManager *)manager
didExitRegion:(CLRegion *)region
{
[[[UIAlertView alloc] initWithTitle:@"区域检测提示"
message:@"您已经【离开】疯狂软件教育中心区域!" delegate:nil
cancelButtonTitle:@"OK" otherButtonTitles:nil] show];
}
@
- CLRegion监听区域
- iOS 硬件 定位 - 区域监听
- ListView 滚动监听 可见区域
- android在指定区域监听滑动事件
- Android 自定义View 分区域监听 回调
- swift 地图定位(六)区域监听
- mapabc高德地图区域收缩事件监听
- CoreLocation框架的基本使用—区域监听
- 实用知识:CoreLocation的使用(区域监听)
- Android对屏幕空白区域的监听方法:
- android朋友圈监听键盘状态 点击空白区域隐藏键盘
- 树的可见区域的获取和滚动条监听
- 区域
- 监听用户进入,离开某个区域,需要请求获取用户的位置,如果想在后台也能监听,需要配置ios9后台监听
- popwindow点击外部区域 返回键 关闭,关闭时候的监听
- (七十六)CoreLocation(二)获取经纬度、速度、方向,进行区域监听
- android 监听软键盘弹起和关闭状态与点击空白区域软键盘隐藏
- 监听
- Dozer轻松实现对象间属性复制
- android 5.0后对于apk 跑32 64 的逻辑
- U8Server——分布式环境下唯一订单号生成规则
- C语言程序设计---10:利用文件保存数据
- do{...}while(0)的意义和用法
- CLRegion监听区域
- 屏幕底部控件始终在软键盘顶端
- POJ 3468 A Simple Problem with Integers【线段树区间更新入门题】
- JS数据结构案例
- 《JS高程(3)》-第6章笔记(02)
- 欢迎使用CSDN-markdown编辑器
- css{clip:rect}好玩的属性
- 数据结构:散列2(探测法)
- Hive笔记四之MR任务