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

       不要使用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协议时可根据需要实现协议中特定的方法。

调用CLLocationManagerstartMonitoringForRegion:方法进行区域监测。区域监测结束时,可调用stopMonitoringForRegion:方法结束区域监测。

当设备进入指定区域时,iOS系统将会自动激发CLLocationManagerdelegate对象的locationManager:didEnterRegion:方法;当设备离开指定区域时,iOS系统将会自动激发CLLocationManagerdelegate对象的locationManager:didExitRegion:方法,开发者可重写这两个方法对用户进行提醒。

iOS提供了CLRegion来定义被监测的区域,但实际编程中推荐使用CLCircularRegionCLRegion的子类)创建圆形区域,创建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];

}

@

0 0