TerminalMapVC.m 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. //
  2. // TerminalMapVC.m
  3. // LN_School
  4. //
  5. // Created by apple on 2017/4/13.
  6. // Copyright © 2017年 Danson. All rights reserved.
  7. //
  8. #import "TerminalMapVC.h"
  9. #import "TerminalMapVC.h"
  10. #import "TerminalListVC.h"
  11. #import <BaiduMapAPI_Map/BMKMapComponent.h>//引入地图功能所有的头文件
  12. #import <BMKLocationkit/BMKLocationComponent.h>//引入定位功能所有的头文件(新)
  13. #import "MyAnnotationView.h"//自定义AnnotationView
  14. #import "BMKClusterManager.h"//点聚合管理类
  15. /*
  16. *点聚合Annotation
  17. */
  18. @interface ClusterAnnotation : BMKPointAnnotation
  19. ///所包含annotation个数
  20. @property (nonatomic, assign) NSInteger size;
  21. @end
  22. @implementation ClusterAnnotation
  23. @synthesize size = _size;
  24. @end
  25. @interface TerminalMapVC ()<BMKMapViewDelegate,BMKLocationManagerDelegate>
  26. {
  27. NSArray *dataArray;
  28. BOOL hasCenter;
  29. BMKMapView *_mapView;
  30. //点聚合
  31. BMKClusterManager *_clusterManager;//点聚合管理类
  32. NSInteger _clusterZoom;//聚合级别
  33. NSMutableArray *_clusterCaches;//点聚合缓存标注
  34. //地图定位
  35. BMKLocationManager *_locService;
  36. }
  37. @property (nonatomic, strong) BMKUserLocation *userLocation;//当前位置对象
  38. @end
  39. @implementation TerminalMapVC
  40. - (void)viewDidLoad {
  41. [super viewDidLoad];
  42. self.title = @"终端管理";
  43. self.view.backgroundColor = KBackGroundColor;
  44. [self goBackByNavigation];
  45. dataArray = [[NSArray alloc]init];
  46. hasCenter = NO;
  47. [self createMap];
  48. // [self createClus];//点聚合
  49. [self getPoint];
  50. }
  51. -(void)viewWillAppear:(BOOL)animated
  52. {
  53. [super viewWillAppear:animated];
  54. [_mapView viewWillAppear];
  55. _mapView.delegate = self; // 此处记得不用的时候需要置nil,否则影响内存的释放
  56. }
  57. -(void)viewWillDisappear:(BOOL)animated
  58. {
  59. [super viewWillDisappear:animated];
  60. [_mapView viewWillDisappear];
  61. _mapView.delegate = nil; // 不用时,置nil
  62. }
  63. -(void)createRightBtn{
  64. self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"列表查看" style:UIBarButtonItemStylePlain target:self action:@selector(gotoList)];
  65. self.navigationItem.rightBarButtonItem.tintColor = RQMianColor;
  66. }
  67. -(void)gotoList{
  68. TerminalListVC *vc = [[TerminalListVC alloc]init];
  69. vc.dataArr = dataArray;
  70. [self navPushHideTabbarToVC:vc];
  71. }
  72. #pragma mark 地图
  73. -(void)createMap{
  74. _mapView = [[BMKMapView alloc]initWithFrame:kFrame];
  75. _mapView.mapType = BMKMapTypeStandard;
  76. _mapView.zoomLevel = 20;
  77. _mapView.showsUserLocation = YES;
  78. [self.view addSubview:_mapView];
  79. //定位功能
  80. _locService = [[BMKLocationManager alloc]init];
  81. _locService.distanceFilter = 10;//将被通知任何移动(若=10,表示超出10米才会回调位置信息)
  82. _locService.delegate = self;
  83. }
  84. //-(void)createClus{
  85. //
  86. // _clusterCaches = [[NSMutableArray alloc] init];
  87. // for (NSInteger i = 3; i < 22; i++) {
  88. // [_clusterCaches addObject:[NSMutableArray array]];
  89. // }
  90. //
  91. // //点聚合管理类
  92. // _clusterManager = [[BMKClusterManager alloc] init];
  93. //}
  94. //向点聚合管理类中添加标注
  95. //- (void)addPointAnnotationsWithArray:(NSArray *)array{
  96. //
  97. // for (int i=0; i<array.count; i++) {
  98. //
  99. // if ([array[i][@"GPI_BD_LATITUDE"] length] < 1 || [array[i][@"GPI_BD_LONGITUDE"] length] < 1) {
  100. // continue;
  101. // }
  102. //
  103. // BMKClusterItem *clusterItem = [[BMKClusterItem alloc] init];
  104. // CLLocationCoordinate2D coor;
  105. // coor.latitude = [[NSString stringWithFormat:@"%@",array[i][@"GPI_BD_LATITUDE"]] floatValue];
  106. // coor.longitude = [[NSString stringWithFormat:@"%@",array[i][@"GPI_BD_LONGITUDE"]] floatValue];
  107. // if ([array[i][@"GPI_BD_LATITUDE"] floatValue] > [array[i][@"GPI_BD_LONGITUDE"] floatValue]) {
  108. // //这种情况是服务器经纬度反了 @RQ-MARK
  109. // coor = CLLocationCoordinate2DMake([array[i][@"GPI_BD_LONGITUDE"] floatValue], [array[i][@"GPI_BD_LATITUDE"] floatValue]);
  110. // }
  111. //
  112. // clusterItem.coor = coor;
  113. // clusterItem.title = [NSString stringWithFormat:@"%@and%@",array[i][@"TCO_LICNUM"],array[i][@"TDI_IS_CONNECT"]];
  114. // clusterItem.subTitle = [NSString stringWithFormat:@"%d",i];
  115. //
  116. // [_clusterManager addClusterItem:clusterItem];
  117. //
  118. // }
  119. //
  120. // [self updateClusters];
  121. //}
  122. ////更新聚合状态
  123. //- (void)updateClusters {
  124. // _clusterZoom = (NSInteger)_mapView.zoomLevel;
  125. // @synchronized(_clusterCaches) {
  126. // __block NSMutableArray *clusters = [_clusterCaches objectAtIndex:(_clusterZoom - 3)];
  127. //
  128. // if (clusters.count > 0) {
  129. // [_mapView removeAnnotations:_mapView.annotations];
  130. // [_mapView addAnnotations:clusters];
  131. // } else {
  132. // dispatch_async(dispatch_get_global_queue(0, 0), ^{
  133. //
  134. // ///获取聚合后的标注
  135. // __block NSArray *array = [_clusterManager getClusters:_clusterZoom];
  136. //
  137. // dispatch_async(dispatch_get_main_queue(), ^{
  138. // for (BMKCluster *item in array) {
  139. // ClusterAnnotation *annotation = [[ClusterAnnotation alloc] init];
  140. // annotation.coordinate = item.coordinate;
  141. // annotation.size = item.size;
  142. // annotation.title = item.title;
  143. // annotation.subtitle = item.subTitle;
  144. // [clusters addObject:annotation];
  145. // NSLog(@"zzzz : %ld",item.size);
  146. // if (!hasCenter) {
  147. // hasCenter = YES;
  148. // [_mapView setCenterCoordinate:annotation.coordinate];
  149. // }
  150. // }
  151. // [_mapView removeAnnotations:_mapView.annotations];
  152. // [_mapView addAnnotations:clusters];
  153. // });
  154. // });
  155. // }
  156. // }
  157. //}
  158. //标注的代理
  159. -(BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id<BMKAnnotation>)annotation
  160. {
  161. if ([annotation isKindOfClass:[BMKPointAnnotation class]]) {
  162. MyAnnotationView *annotationView = [MyAnnotationView annotationViewWithMapView:mapView Annotation:annotation];
  163. // ClusterAnnotation *cluster = (ClusterAnnotation*)annotation;
  164. // //size依赖annotion
  165. // annotationView.annotation = cluster;
  166. // NSLog(@"viewForAnnotation: %@ and %ld",annotationView.annotationImageView,(long)cluster.size);
  167. annotationView.size = 1;//cluster.size
  168. annotationView.dic = dataArray[[annotation.subtitle integerValue]];
  169. return annotationView;
  170. }
  171. return nil;
  172. }
  173. /**
  174. *当选中一个annotation views时,调用此接口
  175. *@param mapView 地图View
  176. *@param view 选中的annotation views
  177. */
  178. //- (void)mapView:(BMKMapView *)mapView didSelectAnnotationView:(BMKAnnotationView *)view{
  179. // if ([view isKindOfClass:[MyAnnotationView class]]) {
  180. // ClusterAnnotation *clusterAnnotation = (ClusterAnnotation*)view.annotation;
  181. // if (clusterAnnotation.size > 1) {
  182. // [mapView setCenterCoordinate:view.annotation.coordinate];
  183. // [mapView zoomIn];
  184. // }
  185. // }
  186. //}
  187. /**
  188. *地图初始化完毕时会调用此接口
  189. *@param mapView 地图View
  190. */
  191. //- (void)mapViewDidFinishLoading:(BMKMapView *)mapView {
  192. // [self updateClusters];
  193. //}
  194. /**
  195. *地图渲染每一帧画面过程中,以及每次需要重绘地图时(例如添加覆盖物)都会调用此接口
  196. *@param mapView 地图View
  197. *@param status 此时地图的状态
  198. */
  199. //- (void)mapView:(BMKMapView *)mapView onDrawMapFrame:(BMKMapStatus *)status {
  200. // if (_clusterZoom != 0 && _clusterZoom != (NSInteger)mapView.zoomLevel) {
  201. // [self updateClusters];
  202. // }
  203. //}
  204. #pragma mark 地图2(去除点聚合)
  205. //添加标注
  206. - (void)addPointAnnotationsWithArray2:(NSArray *)array{
  207. NSMutableArray *arr = [NSMutableArray new];
  208. for (int i=0; i<array.count; i++) {
  209. if ([array[i][@"GPI_BD_LATITUDE"] length] < 1 || [array[i][@"GPI_BD_LONGITUDE"] length] < 1) {
  210. continue;
  211. }
  212. BMKPointAnnotation *_pointAnnotation = [[BMKPointAnnotation alloc]init];
  213. CLLocationCoordinate2D coor;
  214. coor.latitude = [[NSString stringWithFormat:@"%@",array[i][@"GPI_BD_LATITUDE"]] floatValue];
  215. coor.longitude = [[NSString stringWithFormat:@"%@",array[i][@"GPI_BD_LONGITUDE"]] floatValue];
  216. _pointAnnotation.coordinate = coor;
  217. _pointAnnotation.title = [NSString stringWithFormat:@"%@and%@",array[i][@"TCO_LICNUM"],array[i][@"TDI_IS_CONNECT"]];
  218. _pointAnnotation.subtitle = [NSString stringWithFormat:@"%d",i];
  219. [arr addObject:_pointAnnotation];
  220. // NSLog(@"%f,%f",coor.latitude,coor.longitude);
  221. //以第一个点为中点
  222. if (!hasCenter) {
  223. hasCenter = YES;
  224. _mapView.centerCoordinate = coor;
  225. }
  226. }
  227. [_mapView addAnnotations:arr];
  228. }
  229. #pragma mark 定位相关
  230. /**用户拒绝开启定位服务等原因导致的定位失败会调用的方法
  231. */
  232. - (void)BMKLocationManager:(BMKLocationManager *)manager didFailWithError:(NSError *)error {
  233. //[LoadingView showMsg:@"拒绝将无法自动获取当前位置!"];
  234. UIAlertView *alert =[[UIAlertView alloc]initWithTitle:@"温馨提示" message:@"未打开位置访问权限" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles: nil];//@"请在iPhone的“设置”-“隐私”-“定位服务”功能中,找到“极速驾培”打开位置访问权限"
  235. [alert show];
  236. [_locService stopUpdatingLocation];
  237. }
  238. //处理位置坐标更新
  239. - (void)BMKLocationManager:(BMKLocationManager *)manager didUpdateLocation:(BMKLocation *)location orError:(NSError *)error {
  240. _mapView.showsUserLocation = YES;//显示定位图层
  241. self.userLocation.location = location.location;
  242. [_mapView updateLocationData:_userLocation];
  243. _mapView.centerCoordinate = _userLocation.location.coordinate;
  244. }
  245. #pragma mark 网络请求
  246. - (void)getPoint {
  247. if (![NetManager connectedToNetWork]) {
  248. showMsgUnconnect();
  249. return;
  250. }
  251. //@RQ-MARK 1.0.2修复:“终端管理”模块接口(“devs”)调用异常(报“504”)导致功能无法正常应用
  252. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  253. [dic setObject:defUser.userDict[@"school"] forKey:@"schoolId"];
  254. NSString *method = @"devs";
  255. [NetManager requestAnythingWithURL:method dictionary:dic dataArray:nil completion:^(NSDictionary *root) {
  256. if (!root) {
  257. ShowMsg(@"数据请求失败,请重试");
  258. return;
  259. }
  260. if ([root[@"code"] integerValue] == 1) {
  261. ShowMsg(root[@"msg"]);
  262. return;
  263. }
  264. dataArray = root[@"body"];
  265. if (dataArray == 0) {
  266. [_locService stopUpdatingLocation];
  267. return;
  268. }
  269. [self createRightBtn];
  270. [self addPointAnnotationsWithArray2:dataArray];
  271. }];
  272. }
  273. - (BMKUserLocation *)userLocation {
  274. if (!_userLocation) {
  275. _userLocation = [[BMKUserLocation alloc] init];
  276. }
  277. return _userLocation;
  278. }
  279. @end