// // TerminalMapVC.m // LN_School // // Created by apple on 2017/4/13. // Copyright © 2017年 Danson. All rights reserved. // #import "TerminalMapVC.h" #import "TerminalMapVC.h" #import "TerminalListVC.h" #import //引入地图功能所有的头文件 #import //引入定位功能所有的头文件(新) #import "MyAnnotationView.h"//自定义AnnotationView #import "BMKClusterManager.h"//点聚合管理类 #define IS_Cluster NO //是否开启点聚合,另外全局搜索 “@点聚合” /* *点聚合Annotation */ @interface ClusterAnnotation : BMKPointAnnotation ///所包含annotation个数 @property (nonatomic, assign) NSInteger size; @end @implementation ClusterAnnotation @synthesize size = _size; @end @interface TerminalMapVC () { NSArray *dataArray; BOOL hasCenter;//是否已经设置中心点 BMKMapView *_mapView; //点聚合 BMKClusterManager *_clusterManager;//点聚合管理类 NSInteger _clusterZoom;//聚合级别 NSMutableArray *_clusterCaches;//点聚合缓存标注 //地图定位 BMKLocationManager *_locService; } @property (nonatomic, strong) BMKUserLocation *userLocation;//当前位置对象 @end @implementation TerminalMapVC - (void)viewDidLoad { [super viewDidLoad]; self.title = @"终端管理"; self.view.backgroundColor = KBackGroundColor; [self goBackByNavigation]; dataArray = [[NSArray alloc]init]; hasCenter = NO; [self createMap]; [self createClus];//点聚合 [self getPoint]; } -(void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; [_mapView viewWillAppear]; _mapView.delegate = self; // 此处记得不用的时候需要置nil,否则影响内存的释放 } -(void)viewWillDisappear:(BOOL)animated { [super viewWillDisappear:animated]; [_mapView viewWillDisappear]; _mapView.delegate = nil; // 不用时,置nil } -(void)createRightBtn{ UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem]; btn.frame = CGRectMake(0, 0, 80, 44); btn.titleLabel.font = [UIFont systemFontOfSize:17]; [btn setTitle:@"列表查看" forState:UIControlStateNormal]; [btn setTitleColor:defGreen forState:UIControlStateNormal]; [btn addTarget:self action:@selector(gotoList) forControlEvents:UIControlEventTouchUpInside]; self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:btn]; } -(void)gotoList{ TerminalListVC *vc = [[TerminalListVC alloc]init]; vc.dataArr = dataArray; vc.block = ^(NSString *section) { NSDictionary *dic = dataArray[[section integerValue]]; CLLocationCoordinate2D coor; coor.latitude = [[NSString stringWithFormat:@"%@",dic[@"GPI_BD_LATITUDE"]] floatValue]; coor.longitude = [[NSString stringWithFormat:@"%@",dic[@"GPI_BD_LONGITUDE"]] floatValue]; if (coor.latitude>53 || coor.latitude<4 || coor.longitude<73 || coor.longitude>135) { ShowMsg(@"坐标值异常"); [_locService startUpdatingLocation]; }else{ [_mapView setCenterCoordinate:coor animated:YES]; for (ClusterAnnotation *annotation in _mapView.annotations) { if ([annotation.subtitle isEqualToString:section]) { [_mapView selectAnnotation:annotation animated:YES]; } } } }; [self navPushHideTabbarToVC:vc]; } #pragma mark 地图 -(void)createMap{ _mapView = [[BMKMapView alloc]initWithFrame:[UIScreen mainScreen].bounds]; _mapView.mapType = BMKMapTypeStandard; _mapView.isSelectedAnnotationViewFront = YES; _mapView.zoomLevel = 17; _mapView.showsUserLocation = YES; [self.view addSubview:_mapView]; //定位功能 _locService = [[BMKLocationManager alloc]init]; _locService.distanceFilter = 10;//将被通知任何移动(若=10,表示超出10米才会回调位置信息) _locService.delegate = self; } -(void)createClus{ _clusterCaches = [[NSMutableArray alloc] init]; for (NSInteger i = 3; i < 22; i++) { [_clusterCaches addObject:[NSMutableArray array]]; } //点聚合管理类 _clusterManager = [[BMKClusterManager alloc] init]; } //向点聚合管理类中添加标注 - (void)addPointAnnotationsWithArray:(NSArray *)array{ for (int i=0; i coor.longitude) { // //不允许你反转经纬度 // coor = CLLocationCoordinate2DMake([longStr floatValue], [latStr floatValue]); // } if (coor.latitude>53 || coor.latitude<4 || coor.longitude<73 || coor.longitude>135) { continue; } clusterItem.coor = coor; clusterItem.title = [NSString stringWithFormat:@"%@and%@",array[i][@"TCO_LICNUM"],array[i][@"TDI_IS_CONNECT"]]; clusterItem.subTitle = [NSString stringWithFormat:@"%d",i]; [_clusterManager addClusterItem:clusterItem]; } if (IS_Cluster) { [self updateClusters]; }else{ [self updateClusters_other]; } } //更新聚合状态 - (void)updateClusters { _clusterZoom = (NSInteger)_mapView.zoomLevel; @synchronized(_clusterCaches) { __block NSMutableArray *clusters = [_clusterCaches objectAtIndex:_clusterZoom - 3]; if (clusters.count > 0) { [_mapView removeAnnotations:_mapView.annotations]; [_mapView addAnnotations:clusters]; } else { dispatch_async(dispatch_get_global_queue(0, 0), ^{ ///获取聚合后的标注 __block NSArray *array = [_clusterManager getClusters:_clusterZoom]; if (array.count == 0) { [_locService startUpdatingLocation]; return; } dispatch_async(dispatch_get_main_queue(), ^{ for (BMKCluster *item in array) { ClusterAnnotation *annotation = [[ClusterAnnotation alloc] init]; annotation.coordinate = item.coordinate; annotation.size = item.size; annotation.title = item.title; annotation.subtitle = item.subTitle; [clusters addObject:annotation]; // NSLog(@"zzzz : %@",item.subTitle); if (hasCenter == NO) { hasCenter = YES; [_mapView setCenterCoordinate:annotation.coordinate animated:YES]; } } [_mapView removeAnnotations:_mapView.annotations]; [_mapView addAnnotations:clusters]; [_clusterCaches replaceObjectAtIndex:_clusterZoom - 3 withObject:clusters]; }); }); } } } //更新聚合状态(不要点聚合功能调此接口) - (void)updateClusters_other { _clusterZoom = (NSInteger)_mapView.zoomLevel; @synchronized(_clusterCaches) { __block NSMutableArray *clusters = [_clusterCaches objectAtIndex:0]; if (clusters.count == 0) { dispatch_async(dispatch_get_global_queue(0, 0), ^{ ///获取聚合后的标注 __block NSArray *array = [_clusterManager getClusters:_clusterZoom]; if (array.count == 0) { [_locService startUpdatingLocation]; return; } dispatch_async(dispatch_get_main_queue(), ^{ for (BMKCluster *item in array) { ClusterAnnotation *annotation = [[ClusterAnnotation alloc] init]; annotation.coordinate = item.coordinate; annotation.size = item.size; annotation.title = item.title; annotation.subtitle = item.subTitle; [clusters addObject:annotation]; // NSLog(@"zzzz : %ld",item.size); if (!hasCenter) { hasCenter = YES; [_mapView setCenterCoordinate:annotation.coordinate animated:YES]; } } [_mapView removeAnnotations:_mapView.annotations]; [_mapView addAnnotations:clusters]; [_clusterCaches replaceObjectAtIndex:0 withObject:clusters]; }); }); } } } //标注的代理 -(BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id)annotation { if ([annotation isKindOfClass:[BMKPointAnnotation class]]) { MyAnnotationView *annotationView = [MyAnnotationView annotationViewWithMapView:mapView Annotation:annotation]; ClusterAnnotation *cluster = (ClusterAnnotation*)annotation; //size依赖annotion annotationView.annotation = cluster; // NSLog(@"viewForAnnotation: %@ and %ld",annotationView.annotationImageView,(long)cluster.size); annotationView.size = cluster.size; annotationView.dic = dataArray[[annotation.subtitle integerValue]]; return annotationView; } return nil; } /** *当选中一个annotation views时,调用此接口 *@param mapView 地图View *@param view 选中的annotation views */ - (void)mapView:(BMKMapView *)mapView didSelectAnnotationView:(BMKAnnotationView *)view{ if ([view isKindOfClass:[MyAnnotationView class]]) { ClusterAnnotation *clusterAnnotation = (ClusterAnnotation*)view.annotation; if (clusterAnnotation.size > 1) { NSLog(@"%lf,%lf",view.annotation.coordinate.latitude,view.annotation.coordinate.longitude); [mapView setCenterCoordinate:view.annotation.coordinate];// animated:YES 不要使用动画,会影响其它操作 [mapView zoomIn]; } } } /** *地图初始化完毕时会调用此接口 *@param mapView 地图View */ - (void)mapViewDidFinishLoading:(BMKMapView *)mapView { if (IS_Cluster) { [self updateClusters]; }else{ [self updateClusters_other]; } } //点聚合@lee 如果需要点聚合功能的话,就把注释打开 /** *地图渲染每一帧画面过程中,以及每次需要重绘地图时(例如添加覆盖物)都会调用此接口 *@param mapView 地图View *@param status 此时地图的状态 */ - (void)mapView:(BMKMapView *)mapView onDrawMapFrame:(BMKMapStatus *)status { if (_clusterZoom != 0 && _clusterZoom != (NSInteger)mapView.zoomLevel && IS_Cluster) { [self updateClusters]; } } #pragma mark 定位相关 /**用户拒绝开启定位服务等原因导致的定位失败会调用的方法 */ - (void)BMKLocationManager:(BMKLocationManager *)manager didFailWithError:(NSError *)error { UIAlertView *alert =[[UIAlertView alloc]initWithTitle:@"温馨提示" message:@"未打开位置访问权限" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles: nil];//@"请在iPhone的“设置”-“隐私”-“定位服务”功能中,找到“优易学车”打开位置访问权限" [alert show]; [_locService stopUpdatingLocation]; } //处理位置坐标更新 - (void)BMKLocationManager:(BMKLocationManager *)manager didUpdateLocation:(BMKLocation *)location orError:(NSError *)error { _mapView.showsUserLocation = YES;//显示定位图层 self.userLocation.location = location.location; [_mapView updateLocationData:_userLocation]; _mapView.centerCoordinate = _userLocation.location.coordinate; [_locService stopUpdatingLocation]; } #pragma mark 网络请求 - (void)getPoint { if (![NetManager connectedToNetWork]) { showMsgUnconnect(); return; } NSMutableDictionary *dic = [NSMutableDictionary dictionary]; [dic setObject:defUser.userDict[@"school"] forKey:@"schoolId"]; if (_terminalType) { [dic setObject:_terminalType forKey:@"type"]; } NSString *method = @"devs"; [MBProgressHUD showLoadToView:self.view]; [NetManager requestAnythingWithURL:method dictionary:dic dataArray:nil completion:^(NSDictionary *root) { [MBProgressHUD hideHUDForView:self.view]; if (!root) { ShowMsg(@"数据请求失败,请重试"); return; } if ([root[@"code"] integerValue] == 1) { ShowMsg(root[@"msg"]); return; } dataArray = root[@"body"]; if (dataArray == 0) { [_locService startUpdatingLocation]; return; } [self createRightBtn]; [self addPointAnnotationsWithArray:dataArray]; }]; } - (BMKUserLocation *)userLocation { if (!_userLocation) { _userLocation = [[BMKUserLocation alloc] init]; } return _userLocation; } @end