123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412 |
- //
- // 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 <BaiduMapAPI_Map/BMKMapComponent.h>//引入地图功能所有的头文件
- #import <BMKLocationkit/BMKLocationComponent.h>//引入定位功能所有的头文件(新)
- #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 ()<BMKMapViewDelegate,BMKLocationManagerDelegate>
- {
- 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<array.count; i++) {
-
- NSString * latStr = [NSString stringWithFormat:@"%@",array[i][@"GPI_BD_LATITUDE"]];
- NSString * longStr = [NSString stringWithFormat:@"%@",array[i][@"GPI_BD_LONGITUDE"]];
-
- BMKClusterItem *clusterItem = [[BMKClusterItem alloc] init];
- CLLocationCoordinate2D coor;
- coor.latitude = [latStr floatValue];
- coor.longitude = [longStr floatValue];
- // if (coor.latitude > 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<BMKAnnotation>)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
|