CLRegion监听区域
2017-01-10 17:28
134 查看
如果希望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米
不要使用CLRegion
CLRegion *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];
}
@
相关文章推荐
- Extremal Region(极值区域)文本定位与识别法-学习笔记(三)
- C# 将透明图片的非透明区域转换成Region的实例代码
- GdiPlus[41]: IGPRegion 区域
- 利用select_region_point算子可以通过区域内的一个点或者区域的一部分,获取完整区域
- Berkeley DB 区域(region)分析
- ZOJ 3659 Conquer a New Region【长春区域赛E】
- CLBeaconRegion中的notifyEntryStateOnDisplay解释
- android Graphics:区域(Region)
- android在指定区域监听滑动事件
- Android2.3使用BitmapRegionDecoder获取指定区域的图片
- Halcon学习之六:获取Image图像中Region区域的特征参数
- WinAPI: ExtCreateRegion - 区域变换
- 基于对比度滤波的显著区域检测-SF(Saliency Filters: Contrast Based Filtering for Salient Region Detection)
- Android使用BitmapRegionDecoder获取指定区域的图片
- Halcon学习之六:获取Image图像中Region区域的特征参数
- Halcon学习之六:获取Image图像中Region区域的特征参数
- 基于马尔科夫吸收概率的显着区域检测_MAP(Saliency Region Detection Based on Markov Absorption Probabilities)
- 普通Region中动态设置poplist的值跟着当前区域的某些值动态变化
- iOS 区域监听
- iBeacons Tutorial for iOS 7 with CLBeaconRegion and CLBeacon(英文)