// // 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"//点聚合管理类 /* *点聚合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{ self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"列表查看" style:UIBarButtonItemStylePlain target:self action:@selector(gotoList)]; self.navigationItem.rightBarButtonItem.tintColor = RQMianColor; } -(void)gotoList{ TerminalListVC *vc = [[TerminalListVC alloc]init]; vc.dataArr = dataArray; [self navPushHideTabbarToVC:vc]; } #pragma mark 地图 -(void)createMap{ _mapView = [[BMKMapView alloc]initWithFrame:kFrame]; _mapView.mapType = BMKMapTypeStandard; _mapView.zoomLevel = 20; _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 [array[i][@"GPI_BD_LONGITUDE"] floatValue]) { // //这种情况是服务器经纬度反了 @RQ-MARK // coor = CLLocationCoordinate2DMake([array[i][@"GPI_BD_LONGITUDE"] floatValue], [array[i][@"GPI_BD_LATITUDE"] floatValue]); // } // // 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]; // // } // // [self updateClusters]; //} ////更新聚合状态 //- (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]; // // 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]; // } // } // [_mapView removeAnnotations:_mapView.annotations]; // [_mapView addAnnotations: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 = 1;//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) { // [mapView setCenterCoordinate:view.annotation.coordinate]; // [mapView zoomIn]; // } // } //} /** *地图初始化完毕时会调用此接口 *@param mapView 地图View */ //- (void)mapViewDidFinishLoading:(BMKMapView *)mapView { // [self updateClusters]; //} /** *地图渲染每一帧画面过程中,以及每次需要重绘地图时(例如添加覆盖物)都会调用此接口 *@param mapView 地图View *@param status 此时地图的状态 */ //- (void)mapView:(BMKMapView *)mapView onDrawMapFrame:(BMKMapStatus *)status { // if (_clusterZoom != 0 && _clusterZoom != (NSInteger)mapView.zoomLevel) { // [self updateClusters]; // } //} #pragma mark 地图2(去除点聚合) //添加标注 - (void)addPointAnnotationsWithArray2:(NSArray *)array{ NSMutableArray *arr = [NSMutableArray new]; for (int i=0; i