iOS 高德地图 地理围栏
来源:互联网 发布:贝叶斯网络推断 编辑:程序博客网 时间:2024/04/28 23:59
地理围栏是一个(或多个)圆形的地理边界作为虚拟围栏,当设备进入、离开该区域时,可以接收到消息通知。
地理围栏的半径数值需要大于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]; }];}//核心方法//创建圆形的定位区域cirRegion200AMapLocationCircleRegion *cirRegion200 = [[AMapLocationCircleRegion alloc] initWithCenter:coordinate radius:200.0 identifier:@"circleRegion200"];//创建圆形的定位区域cirRegion300AMapLocationCircleRegion *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];//添加OverlayMACircle *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
0 0
- iOS 高德地图 地理围栏
- 高德地图——地理围栏
- Android使用高德地图地理围栏定位自动发送短信
- iOS开发-地图中地理围栏的使用,等待写。。。。
- 百度地图API实现地理围栏
- iOS开发:高德地图显示、定位、反地理编码
- iOS 高德地图反向地理编码 onReGeocodeSearchDone 不回调
- 地理围栏
- iOS开发-定位服务和地理围栏
- 高德地图中如何绘制多边形围栏
- iOS开发——高德地图调研之显示地图、定位与地理编码等
- 高德地图 正向地理编码 逆向地理编码
- 高德地图地理编码和逆地理编码
- 高德地图之地理编码
- 高德地图之逆地理编码
- 基于高德地图的地理编码
- ios 地图大头针固定在地图中心,用用地图中心点左边逆地理编码,及高德云图周边搜索
- IOS-OC-高德地图-反向地理编码 - 坐标转位置
- java volatile 关键字
- hdu1097 A hard puzzle hdu5500 Reorder the Books
- 推荐系统杂谈
- 8天学通MongoDB——第二天 细说增删查改
- CopyOnWriteArrayList
- iOS 高德地图 地理围栏
- WORD 是2Bytes还是4bytes?
- Hibernate 查询HQL,QBC 及命名HQL,SQL查询
- 获取App的权限列表,及判断添加的权限有没有生效
- linux Vi编辑器的使用
- SAP-ABAP三种定义嵌套型结构的方法
- 伺服运动控制学习
- ReentrantLock 线程锁
- extjs store 请求返回值为上一次请求的返回值