温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

疯狂ios讲义之使用CoreLocation定位(4)

发布时间:2020-07-16 20:47:56 来源:网络 阅读:577 作者:fkJava李刚 栏目:移动开发

疯狂ios讲义之使用CoreLocation定位(4)9.4区域监测

如果希望iOS设备进入某个区域发出通知,那么这种区域监测的功能也被称为临近警告。所谓临近警告的示意图如图9.6所示。

疯狂ios讲义之使用CoreLocation定位(4)

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对象时无非就是指定圆心、半径等信息,非常简单。下面示例会进行详细示范。

新建一个SingleView Application,该应用无须修改界面设计文件,直接修改视图控制器类的实现部分来监测设备是否进入、离开某个区域。该示例的视图控制器类的实现部分代码如下。

程序清单:codes/09/9.4/RegionMonitor/RegionMonitor/FKViewController.m

@interface FKViewController ()<CLLocationManagerDelegate>

@property (retain,nonatomic) CLLocationManager*locationManager;

@end

@implementation FKViewController

- (void)viewDidLoad

{

[superviewDidLoad];

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 = [[CLCircularRegionalloc] initWithCenter:companyCenter

radius:500 identifier:@"fkit"];

// 开始监听fkit区域

[self.locationManagerstartMonitoringForRegion: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];

}

@end


正如上面程序中的粗体字代码所看到的,第1行粗体字代码创建了一个CLRegion对象作为被监测区域,接下来调用CLLocationManagerstartMonitoringForRegion:方法监听该设备是否进入/离开指定区域。

当该设备进入或离开指定区域时,iOS系统都会自动激发CLLocationManagerdelegate对象的相应方法,上面程序重写了这两个方法对用户进行提醒。

编译、运行该程序,如果将模拟器的位置设为{23.126272, 113.395568},此时设备将会进入“疯狂软件教育中心区域内”,系统将会显示如图9.7所示的提示。

编译、运行该程序,如果将模拟器的位置设为其他位置,使之离开{23.126272, 113.395568}超过500米,此时设备将会离开“疯狂软件教育中心区域内”,系统将会显示如图9.8所示的提示。

疯狂ios讲义之使用CoreLocation定位(4)

9.7进入区域提示

疯狂ios讲义之使用CoreLocation定位(4)

9.8离开区域提示


向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI