123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339 |
- //
- // 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"//点聚合管理类
- /*
- *点聚合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{
-
- 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.count; i++) {
- //
- // if ([array[i][@"GPI_BD_LATITUDE"] length] < 1 || [array[i][@"GPI_BD_LONGITUDE"] length] < 1) {
- // continue;
- // }
- //
- // BMKClusterItem *clusterItem = [[BMKClusterItem alloc] init];
- // CLLocationCoordinate2D coor;
- // coor.latitude = [[NSString stringWithFormat:@"%@",array[i][@"GPI_BD_LATITUDE"]] floatValue];
- // coor.longitude = [[NSString stringWithFormat:@"%@",array[i][@"GPI_BD_LONGITUDE"]] floatValue];
- // if ([array[i][@"GPI_BD_LATITUDE"] floatValue] > [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<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 = 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<array.count; i++) {
- if ([array[i][@"GPI_BD_LATITUDE"] length] < 1 || [array[i][@"GPI_BD_LONGITUDE"] length] < 1) {
- continue;
- }
- BMKPointAnnotation *_pointAnnotation = [[BMKPointAnnotation alloc]init];
- CLLocationCoordinate2D coor;
- coor.latitude = [[NSString stringWithFormat:@"%@",array[i][@"GPI_BD_LATITUDE"]] floatValue];
- coor.longitude = [[NSString stringWithFormat:@"%@",array[i][@"GPI_BD_LONGITUDE"]] floatValue];
- _pointAnnotation.coordinate = coor;
- _pointAnnotation.title = [NSString stringWithFormat:@"%@and%@",array[i][@"TCO_LICNUM"],array[i][@"TDI_IS_CONNECT"]];
- _pointAnnotation.subtitle = [NSString stringWithFormat:@"%d",i];
-
- [arr addObject:_pointAnnotation];
- // NSLog(@"%f,%f",coor.latitude,coor.longitude);
- //以第一个点为中点
- if (!hasCenter) {
- hasCenter = YES;
- _mapView.centerCoordinate = coor;
- }
- }
- [_mapView addAnnotations:arr];
- }
- #pragma mark 定位相关
- /**用户拒绝开启定位服务等原因导致的定位失败会调用的方法
- */
- - (void)BMKLocationManager:(BMKLocationManager *)manager didFailWithError:(NSError *)error {
- //[LoadingView showMsg:@"拒绝将无法自动获取当前位置!"];
- 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;
- }
- #pragma mark 网络请求
- - (void)getPoint {
- if (![NetManager connectedToNetWork]) {
- showMsgUnconnect();
- return;
- }
- //@RQ-MARK 1.0.2修复:“终端管理”模块接口(“devs”)调用异常(报“504”)导致功能无法正常应用
- NSMutableDictionary *dic = [NSMutableDictionary dictionary];
- [dic setObject:defUser.userDict[@"school"] forKey:@"schoolId"];
-
- NSString *method = @"devs";
- [NetManager requestAnythingWithURL:method dictionary:dic dataArray:nil completion:^(NSDictionary *root) {
-
- if (!root) {
- ShowMsg(@"数据请求失败,请重试");
- return;
- }
- if ([root[@"code"] integerValue] == 1) {
- ShowMsg(root[@"msg"]);
- return;
- }
-
-
- dataArray = root[@"body"];
-
- if (dataArray == 0) {
- [_locService stopUpdatingLocation];
- return;
- }
-
- [self createRightBtn];
- [self addPointAnnotationsWithArray2:dataArray];
-
- }];
- }
- - (BMKUserLocation *)userLocation {
- if (!_userLocation) {
- _userLocation = [[BMKUserLocation alloc] init];
- }
- return _userLocation;
- }
- @end
|