您的位置:首页 > 移动开发 > IOS开发

ios 定位 监听是否跨入某个指定的区域

2015-09-15 19:39 507 查看
/*****监听用户是否进入和走出 在某个区域*****/
1 #import "ViewController.h"
#import <CoreLocation/CoreLocation.h>

@interface ViewController ()<CLLocationManagerDelegate>

/** */
@property (nonatomic, strong) CLLocationManager *locationM;

@property (weak, nonatomic) IBOutlet UILabel *notice;
@end

@implementation ViewController

#pragma mark - 懒加载
/** 位置管理者属性的懒加载 */
-(CLLocationManager *)locationM
{
if (!_locationM) {
_locationM = [[CLLocationManager alloc] init];
_locationM.delegate = self;

if([[UIDevice currentDevice].systemVersion floatValue] >= 8.0)
[_locationM requestAlwaysAuthorization];

}
return _locationM;
}

- (void)viewDidLoad {

// 参数, 必须继承自  clregion
if ([CLLocationManager isMonitoringAvailableForClass:[CLCircularRegion class]]) {

// 1. 创建区域
// 1. 1 确定区域中心
CLLocationCoordinate2D center = {21.123, 121.345};

// 1.2 区域半径
CLLocationDistance distance = 1000;
//2128000.000000  最大监控限度
if (distance > self.locationM.maximumRegionMonitoringDistance) {
distance = self.locationM.maximumRegionMonitoringDistance;
}

CLCircularRegion *region = [[CLCircularRegion alloc] initWithCenter:center radius:distance identifier:@"一个神秘的地方"];

/**以下两种方式都能监听 是否在区域 里面,建议用监控状态的方式 .下面两种方式都写出来了**/

//-------######-----(方式一 start)------------
54
55     // 2. 监听区域(会调用 didEnterRegion  和 didExitRegion 方法)
56     //  [self.locationM startMonitoringForRegion:region];
57         //-------######-----(方式一 end )------------

//-------######-----(方式二 start)------------

// 请求某个区域当前状态(在后台也可以监听到某个区域)
63         /***
64          CLRegionStateUnknown,
65          CLRegionStateInside,
66          CLRegionStateOutside
67          **/
68
69      // 2. 监听状态(会调用 didDetermineState 方法)
70         [self.locationM requestStateForRegion:region];

//------######------(方式二 end)------------

}

}

#pragma mark - CLLocationManagerDelegate 代理方法

/**
*  进入区域
*
*  @param manager 位置管理者
*  @param region  区域
*/
-(void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region
{
NSLog(@"进入区域---%@", region.identifier);
self.notice.text = @"欢迎来到一个神秘的地方, 给你钱";
}

/**
*  离开区域
*
*  @param manager 位置管理者
*  @param region  区域
*/
-(void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region
{
NSLog(@"离开区域---%@", region.identifier);
self.notice.text = @"祝你们有大好前途";
}
/**
*  注册区域失败调用
*
*  @param manager 位置管理者
*  @param region  区域
*  @param error   错误信息
*/
-(void)locationManager:(CLLocationManager *)manager monitoringDidFailForRegion:(CLRegion *)region withError:(NSError *)error
{

// 移除距离较远的region
//    [self.locationM stopMonitoringForRegion:region比较远的region ]

}

/**
*  当我们请求区域状态时调用
*
*  @param manager 位置管理者
*  @param state   状态
*  @param region  区域
*/
-(void)locationManager:(CLLocationManager *)manager didDetermineState:(CLRegionState)state forRegion:(CLRegion *)region
{
if(state == CLRegionStateInside)
{
self.notice.text = @"欢迎来到一个神秘的地方, 给你钱";
}

if (state == CLRegionStateOutside) {
self.notice.text = @"祝你们有大好前途";

}
}

@end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: