地理围栏是一个(或多个)圆形的地理边界作为虚拟围栏,当设备进入、离开该区域时,可以接收到消息通知。
地理围栏的半径数值需要大于0,如果 APP 退出,围栏随即失效。
//使用说明
//包含的头文件
#import <MAMapKit/MAMapKit.h>
#import <AMapLocationKit/AMapLocationKit.h>
//视图控制器需要遵循的协议
<MAMapViewDelegate, AMapLocationManagerDelegate>
//视图控制器需要的属性
@property (nonatomic, strong) MAMapView *mapView;
@property (nonatomic, strong) AMapLocationManager *locationManager;
@property (nonatomic, strong) NSMutableArray *regions;
//应初始化的内容
- (void)viewDidLoad
{
[super viewDidLoad];
[self.view setBackgroundColor:[UIColor whiteColor]];
[self initMapView];
[self configLocationManager];
self.regions = [[NSMutableArray alloc] init];
self.mapView.showsUserLocation = YES;
}
//初始化地图
- (void)initMapView
{
if (self.mapView == nil)
{
self.mapView = [[MAMapView alloc] initWithFrame:self.view.bounds];
[self.mapView setDelegate:self];
[self.view addSubview:self.mapView];
}
}
//初始化位置管理器
- (void)configLocationManager
{
self.locationManager = [[AMapLocationManager alloc] init];
[self.locationManager setDelegate:self];
//设置期望定位精度
[self.locationManager setDesiredAccuracy:kCLLocationAccuracyHundredMeters];
//设置不允许系统暂停定位
[self.locationManager setPausesLocationUpdatesAutomatically:NO];
//设置允许在后台定位
[self.locationManager setAllowsBackgroundLocationUpdates:YES];
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
self.navigationController.toolbar.translucent = YES;
self.navigationController.toolbarHidden = YES;
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
[self getCurrentLocation];
}
//视图消失时应实现的内容
- (void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
//停止地理围栏
[self.regions enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
[self.locationManager stopMonitoringForRegion:(AMapLocationRegion *)obj];
}];
}
//核心方法
//创建圆形的定位区域cirRegion200
AMapLocationCircleRegion *cirRegion200 = [[AMapLocationCircleRegion alloc] initWithCenter:coordinate
radius:200.0
identifier:@"circleRegion200"];
//创建圆形的定位区域cirRegion300
AMapLocationCircleRegion *cirRegion300 = [[AMapLocationCircleRegion alloc] initWithCenter:coordinate
radius:300.0
identifier:@"circleRegion300"];
//添加地理围栏
[self.locationManager startMonitoringForRegion:cirRegion200];
[self.locationManager startMonitoringForRegion:cirRegion300];
//保存地理围栏
[self.regions addObject:cirRegion200];
[self.regions addObject:cirRegion300];
//添加Overlay
MACircle *circle200 = [MACircle circleWithCenterCoordinate:coordinate radius:200.0];
MACircle *circle300 = [MACircle circleWithCenterCoordinate:coordinate radius:300.0];
[self.mapView addOverlay:circle200];
[self.mapView addOverlay:circle300];
[self.mapView setVisibleMapRect:circle300.boundingMapRect];
//接收地理围栏回调
- (void)amapLocationManager:(AMapLocationManager *)manager didEnterRegion:(AMapLocationRegion *)region
{
NSLog(@"进入围栏:%@", region);
}
- (void)amapLocationManager:(AMapLocationManager *)manager didExitRegion:(AMapLocationRegion *)region
{
NSLog(@"走出围栏:%@", region);
}
//--------------------------------------------
.h
#import <UIKit/UIKit.h>
#import <MAMapKit/MAMapKit.h>
#import <AMapLocationKit/AMapLocationKit.h>
@interface MonitoringRegionViewController : UIViewController
@property (nonatomic, strong) MAMapView *mapView;
@property (nonatomic, strong) AMapLocationManager *locationManager;
@end
.m
#import "MonitoringRegionViewController.h"
@interface MonitoringRegionViewController ()<MAMapViewDelegate, AMapLocationManagerDelegate>
@property (nonatomic, strong) NSMutableArray *regions;
@end
@implementation MonitoringRegionViewController
#pragma mark - Add Regions
- (void)getCurrentLocation
{
//获取一次用户的当前位置,方便添加地理围栏
__weak typeof(self) weakSelf = self;
[self.locationManager requestLocationWithReGeocode:NO completionBlock:^(CLLocation *location, AMapLocationReGeocode *regeocode, NSError *error) {
[weakSelf addCircleReionForCoordinate:location.coordinate];
}];
}
- (void)addCircleReionForCoordinate:(CLLocationCoordinate2D)coordinate
{
//创建圆形地理围栏
AMapLocationCircleRegion *cirRegion200 = [[AMapLocationCircleRegion alloc] initWithCenter:coordinate
radius:200.0
identifier:@"circleRegion200"];
AMapLocationCircleRegion *cirRegion300 = [[AMapLocationCircleRegion alloc] initWithCenter:coordinate
radius:300.0
identifier:@"circleRegion300"];
//添加地理围栏
[self.locationManager startMonitoringForRegion:cirRegion200];
[self.locationManager startMonitoringForRegion:cirRegion300];
//保存地理围栏
[self.regions addObject:cirRegion200];
[self.regions addObject:cirRegion300];
//添加地理围栏对应的Overlay,方便查看
MACircle *circle200 = [MACircle circleWithCenterCoordinate:coordinate radius:200.0];
MACircle *circle300 = [MACircle circleWithCenterCoordinate:coordinate radius:300.0];
[self.mapView addOverlay:circle200];
[self.mapView addOverlay:circle300];
[self.mapView setVisibleMapRect:circle300.boundingMapRect];
}
#pragma mark - Action Handle
- (void)configLocationManager
{
self.locationManager = [[AMapLocationManager alloc] init];
[self.locationManager setDelegate:self];
//设置期望定位精度
[self.locationManager setDesiredAccuracy:kCLLocationAccuracyHundredMeters];
//设置不允许系统暂停定位
[self.locationManager setPausesLocationUpdatesAutomatically:NO];
//设置允许在后台定位
[self.locationManager setAllowsBackgroundLocationUpdates:YES];
}
#pragma mark - AMapLocationManagerDelegate
- (void)amapLocationManager:(AMapLocationManager *)manager didFailWithError:(NSError *)error
{
NSLog(@"locationError:{%ld;%@}", (long)error.code, error.localizedDescription);
}
- (void)amapLocationManager:(AMapLocationManager *)manager didStartMonitoringForRegion:(AMapLocationRegion *)region
{
NSLog(@"didStartMonitoringForRegion:%@", region);
}
- (void)amapLocationManager:(AMapLocationManager *)manager monitoringDidFailForRegion:(AMapLocationRegion *)region withError:(NSError *)error
{
NSLog(@"monitoringDidFailForRegion:%@", error.localizedDescription);
}
- (void)amapLocationManager:(AMapLocationManager *)manager didEnterRegion:(AMapLocationRegion *)region
{
NSLog(@"didEnterRegion:%@", region);
}
- (void)amapLocationManager:(AMapLocationManager *)manager didExitRegion:(AMapLocationRegion *)region
{
NSLog(@"didExitRegion:%@", region);
}
- (void)amapLocationManager:(AMapLocationManager *)manager didDetermineState:(AMapLocationRegionState)state forRegion:(AMapLocationRegion *)region
{
NSLog(@"didDetermineState:%@; state:%ld", region, (long)state);
}
#pragma mark - Initialization
- (void)initMapView
{
if (self.mapView == nil)
{
self.mapView = [[MAMapView alloc] initWithFrame:self.view.bounds];
[self.mapView setDelegate:self];
[self.view addSubview:self.mapView];
}
}
#pragma mark - Life Cycle
- (void)viewDidLoad
{
[super viewDidLoad];
[self.view setBackgroundColor:[UIColor whiteColor]];
[self initMapView];
[self configLocationManager];
self.regions = [[NSMutableArray alloc] init];
self.mapView.showsUserLocation = YES;
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
self.navigationController.toolbar.translucent = YES;
self.navigationController.toolbarHidden = YES;
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
[self getCurrentLocation];
}
- (void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
//停止地理围栏
[self.regions enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
[self.locationManager stopMonitoringForRegion:(AMapLocationRegion *)obj];
}];
}
#pragma mark - MAMapViewDelegate
//根据overlay生成对应的Renderer(图像渲染)
- (MAOverlayRenderer *)mapView:(MAMapView *)mapView rendererForOverlay:(id <MAOverlay>)overlay
{
if ([overlay isKindOfClass:[MAPolygon class]])
{
MAPolygonRenderer *polylineRenderer = [[MAPolygonRenderer alloc] initWithPolygon:overlay];
polylineRenderer.lineWidth = 5.0f;
polylineRenderer.strokeColor = [UIColor redColor];
return polylineRenderer;
}
else if ([overlay isKindOfClass:[MACircle class]])
{
MACircleRenderer *circleRenderer = [[MACircleRenderer alloc] initWithCircle:overlay];
circleRenderer.lineWidth = 5.0f;
circleRenderer.strokeColor = [UIColor blueColor];
return circleRenderer;
}
return nil;
}
@end