地理圍欄是一個(或多個)圓形的地理邊界作爲虛擬圍欄,當設備進入、離開該區域時,可以接收到消息通知。
地理圍欄的半徑數值需要大於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