TerminalMapVC.m 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  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. #define IS_Cluster NO //是否开启点聚合,另外全局搜索 “@点聚合”
  16. /*
  17. *点聚合Annotation
  18. */
  19. @interface ClusterAnnotation : BMKPointAnnotation
  20. ///所包含annotation个数
  21. @property (nonatomic, assign) NSInteger size;
  22. @end
  23. @implementation ClusterAnnotation
  24. @synthesize size = _size;
  25. @end
  26. @interface TerminalMapVC ()<BMKMapViewDelegate,BMKLocationManagerDelegate>
  27. {
  28. NSArray *dataArray;
  29. BOOL hasCenter;//是否已经设置中心点
  30. BMKMapView *_mapView;
  31. //点聚合
  32. BMKClusterManager *_clusterManager;//点聚合管理类
  33. NSInteger _clusterZoom;//聚合级别
  34. NSMutableArray *_clusterCaches;//点聚合缓存标注
  35. //地图定位
  36. BMKLocationManager *_locService;
  37. }
  38. @property (nonatomic, strong) BMKUserLocation *userLocation;//当前位置对象
  39. @end
  40. @implementation TerminalMapVC
  41. - (void)viewDidLoad {
  42. [super viewDidLoad];
  43. self.title = @"终端管理";
  44. self.view.backgroundColor = KBackGroundColor;
  45. [self goBackByNavigation];
  46. dataArray = [[NSArray alloc]init];
  47. hasCenter = NO;
  48. [self createMap];
  49. [self createClus];//点聚合
  50. [self getPoint];
  51. }
  52. -(void)viewWillAppear:(BOOL)animated
  53. {
  54. [super viewWillAppear:animated];
  55. [_mapView viewWillAppear];
  56. _mapView.delegate = self; // 此处记得不用的时候需要置nil,否则影响内存的释放
  57. }
  58. -(void)viewWillDisappear:(BOOL)animated
  59. {
  60. [super viewWillDisappear:animated];
  61. [_mapView viewWillDisappear];
  62. _mapView.delegate = nil; // 不用时,置nil
  63. }
  64. -(void)createRightBtn{
  65. UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
  66. btn.frame = CGRectMake(0, 0, 80, 44);
  67. btn.titleLabel.font = [UIFont systemFontOfSize:17];
  68. [btn setTitle:@"列表查看" forState:UIControlStateNormal];
  69. [btn setTitleColor:defGreen forState:UIControlStateNormal];
  70. [btn addTarget:self action:@selector(gotoList) forControlEvents:UIControlEventTouchUpInside];
  71. self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:btn];
  72. }
  73. -(void)gotoList{
  74. TerminalListVC *vc = [[TerminalListVC alloc]init];
  75. vc.dataArr = dataArray;
  76. vc.block = ^(NSString *section) {
  77. NSDictionary *dic = dataArray[[section integerValue]];
  78. CLLocationCoordinate2D coor;
  79. coor.latitude = [[NSString stringWithFormat:@"%@",dic[@"GPI_BD_LATITUDE"]] floatValue];
  80. coor.longitude = [[NSString stringWithFormat:@"%@",dic[@"GPI_BD_LONGITUDE"]] floatValue];
  81. if (coor.latitude>53 || coor.latitude<4 || coor.longitude<73 || coor.longitude>135) {
  82. ShowMsg(@"坐标值异常");
  83. [_locService startUpdatingLocation];
  84. }else{
  85. [_mapView setCenterCoordinate:coor animated:YES];
  86. for (ClusterAnnotation *annotation in _mapView.annotations) {
  87. if ([annotation.subtitle isEqualToString:section]) {
  88. [_mapView selectAnnotation:annotation animated:YES];
  89. }
  90. }
  91. }
  92. };
  93. [self navPushHideTabbarToVC:vc];
  94. }
  95. #pragma mark 地图
  96. -(void)createMap{
  97. _mapView = [[BMKMapView alloc]initWithFrame:[UIScreen mainScreen].bounds];
  98. _mapView.mapType = BMKMapTypeStandard;
  99. _mapView.isSelectedAnnotationViewFront = YES;
  100. _mapView.zoomLevel = 17;
  101. _mapView.showsUserLocation = YES;
  102. [self.view addSubview:_mapView];
  103. //定位功能
  104. _locService = [[BMKLocationManager alloc]init];
  105. _locService.distanceFilter = 10;//将被通知任何移动(若=10,表示超出10米才会回调位置信息)
  106. _locService.delegate = self;
  107. }
  108. -(void)createClus{
  109. _clusterCaches = [[NSMutableArray alloc] init];
  110. for (NSInteger i = 3; i < 22; i++) {
  111. [_clusterCaches addObject:[NSMutableArray array]];
  112. }
  113. //点聚合管理类
  114. _clusterManager = [[BMKClusterManager alloc] init];
  115. }
  116. //向点聚合管理类中添加标注
  117. - (void)addPointAnnotationsWithArray:(NSArray *)array{
  118. for (int i=0; i<array.count; i++) {
  119. NSString * latStr = [NSString stringWithFormat:@"%@",array[i][@"GPI_BD_LATITUDE"]];
  120. NSString * longStr = [NSString stringWithFormat:@"%@",array[i][@"GPI_BD_LONGITUDE"]];
  121. BMKClusterItem *clusterItem = [[BMKClusterItem alloc] init];
  122. CLLocationCoordinate2D coor;
  123. coor.latitude = [latStr floatValue];
  124. coor.longitude = [longStr floatValue];
  125. // if (coor.latitude > coor.longitude) {
  126. // //不允许你反转经纬度
  127. // coor = CLLocationCoordinate2DMake([longStr floatValue], [latStr floatValue]);
  128. // }
  129. if (coor.latitude>53 || coor.latitude<4 || coor.longitude<73 || coor.longitude>135) {
  130. continue;
  131. }
  132. clusterItem.coor = coor;
  133. clusterItem.title = [NSString stringWithFormat:@"%@and%@",array[i][@"TCO_LICNUM"],array[i][@"TDI_IS_CONNECT"]];
  134. clusterItem.subTitle = [NSString stringWithFormat:@"%d",i];
  135. [_clusterManager addClusterItem:clusterItem];
  136. }
  137. if (IS_Cluster) {
  138. [self updateClusters];
  139. }else{
  140. [self updateClusters_other];
  141. }
  142. }
  143. //更新聚合状态
  144. - (void)updateClusters {
  145. _clusterZoom = (NSInteger)_mapView.zoomLevel;
  146. @synchronized(_clusterCaches) {
  147. __block NSMutableArray *clusters = [_clusterCaches objectAtIndex:_clusterZoom - 3];
  148. if (clusters.count > 0) {
  149. [_mapView removeAnnotations:_mapView.annotations];
  150. [_mapView addAnnotations:clusters];
  151. } else {
  152. dispatch_async(dispatch_get_global_queue(0, 0), ^{
  153. ///获取聚合后的标注
  154. __block NSArray *array = [_clusterManager getClusters:_clusterZoom];
  155. if (array.count == 0) {
  156. [_locService startUpdatingLocation];
  157. return;
  158. }
  159. dispatch_async(dispatch_get_main_queue(), ^{
  160. for (BMKCluster *item in array) {
  161. ClusterAnnotation *annotation = [[ClusterAnnotation alloc] init];
  162. annotation.coordinate = item.coordinate;
  163. annotation.size = item.size;
  164. annotation.title = item.title;
  165. annotation.subtitle = item.subTitle;
  166. [clusters addObject:annotation];
  167. // NSLog(@"zzzz : %@",item.subTitle);
  168. if (hasCenter == NO) {
  169. hasCenter = YES;
  170. [_mapView setCenterCoordinate:annotation.coordinate animated:YES];
  171. }
  172. }
  173. [_mapView removeAnnotations:_mapView.annotations];
  174. [_mapView addAnnotations:clusters];
  175. [_clusterCaches replaceObjectAtIndex:_clusterZoom - 3 withObject:clusters];
  176. });
  177. });
  178. }
  179. }
  180. }
  181. //更新聚合状态(不要点聚合功能调此接口)
  182. - (void)updateClusters_other {
  183. _clusterZoom = (NSInteger)_mapView.zoomLevel;
  184. @synchronized(_clusterCaches) {
  185. __block NSMutableArray *clusters = [_clusterCaches objectAtIndex:0];
  186. if (clusters.count == 0) {
  187. dispatch_async(dispatch_get_global_queue(0, 0), ^{
  188. ///获取聚合后的标注
  189. __block NSArray *array = [_clusterManager getClusters:_clusterZoom];
  190. if (array.count == 0) {
  191. [_locService startUpdatingLocation];
  192. return;
  193. }
  194. dispatch_async(dispatch_get_main_queue(), ^{
  195. for (BMKCluster *item in array) {
  196. ClusterAnnotation *annotation = [[ClusterAnnotation alloc] init];
  197. annotation.coordinate = item.coordinate;
  198. annotation.size = item.size;
  199. annotation.title = item.title;
  200. annotation.subtitle = item.subTitle;
  201. [clusters addObject:annotation];
  202. // NSLog(@"zzzz : %ld",item.size);
  203. if (!hasCenter) {
  204. hasCenter = YES;
  205. [_mapView setCenterCoordinate:annotation.coordinate animated:YES];
  206. }
  207. }
  208. [_mapView removeAnnotations:_mapView.annotations];
  209. [_mapView addAnnotations:clusters];
  210. [_clusterCaches replaceObjectAtIndex:0 withObject:clusters];
  211. });
  212. });
  213. }
  214. }
  215. }
  216. //标注的代理
  217. -(BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id<BMKAnnotation>)annotation
  218. {
  219. if ([annotation isKindOfClass:[BMKPointAnnotation class]]) {
  220. MyAnnotationView *annotationView = [MyAnnotationView annotationViewWithMapView:mapView Annotation:annotation];
  221. ClusterAnnotation *cluster = (ClusterAnnotation*)annotation;
  222. //size依赖annotion
  223. annotationView.annotation = cluster;
  224. // NSLog(@"viewForAnnotation: %@ and %ld",annotationView.annotationImageView,(long)cluster.size);
  225. annotationView.size = cluster.size;
  226. annotationView.dic = dataArray[[annotation.subtitle integerValue]];
  227. return annotationView;
  228. }
  229. return nil;
  230. }
  231. /**
  232. *当选中一个annotation views时,调用此接口
  233. *@param mapView 地图View
  234. *@param view 选中的annotation views
  235. */
  236. - (void)mapView:(BMKMapView *)mapView didSelectAnnotationView:(BMKAnnotationView *)view{
  237. if ([view isKindOfClass:[MyAnnotationView class]]) {
  238. ClusterAnnotation *clusterAnnotation = (ClusterAnnotation*)view.annotation;
  239. if (clusterAnnotation.size > 1) {
  240. NSLog(@"%lf,%lf",view.annotation.coordinate.latitude,view.annotation.coordinate.longitude);
  241. [mapView setCenterCoordinate:view.annotation.coordinate];// animated:YES 不要使用动画,会影响其它操作
  242. [mapView zoomIn];
  243. }
  244. }
  245. }
  246. /**
  247. *地图初始化完毕时会调用此接口
  248. *@param mapView 地图View
  249. */
  250. - (void)mapViewDidFinishLoading:(BMKMapView *)mapView {
  251. if (IS_Cluster) {
  252. [self updateClusters];
  253. }else{
  254. [self updateClusters_other];
  255. }
  256. }
  257. //点聚合@lee 如果需要点聚合功能的话,就把注释打开
  258. /**
  259. *地图渲染每一帧画面过程中,以及每次需要重绘地图时(例如添加覆盖物)都会调用此接口
  260. *@param mapView 地图View
  261. *@param status 此时地图的状态
  262. */
  263. - (void)mapView:(BMKMapView *)mapView onDrawMapFrame:(BMKMapStatus *)status {
  264. if (_clusterZoom != 0 && _clusterZoom != (NSInteger)mapView.zoomLevel && IS_Cluster) {
  265. [self updateClusters];
  266. }
  267. }
  268. #pragma mark 定位相关
  269. /**用户拒绝开启定位服务等原因导致的定位失败会调用的方法
  270. */
  271. - (void)BMKLocationManager:(BMKLocationManager *)manager didFailWithError:(NSError *)error {
  272. UIAlertView *alert =[[UIAlertView alloc]initWithTitle:@"温馨提示" message:@"未打开位置访问权限" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles: nil];//@"请在iPhone的“设置”-“隐私”-“定位服务”功能中,找到“优易学车”打开位置访问权限"
  273. [alert show];
  274. [_locService stopUpdatingLocation];
  275. }
  276. //处理位置坐标更新
  277. - (void)BMKLocationManager:(BMKLocationManager *)manager didUpdateLocation:(BMKLocation *)location orError:(NSError *)error {
  278. _mapView.showsUserLocation = YES;//显示定位图层
  279. self.userLocation.location = location.location;
  280. [_mapView updateLocationData:_userLocation];
  281. _mapView.centerCoordinate = _userLocation.location.coordinate;
  282. [_locService stopUpdatingLocation];
  283. }
  284. #pragma mark 网络请求
  285. - (void)getPoint {
  286. if (![NetManager connectedToNetWork]) {
  287. showMsgUnconnect();
  288. return;
  289. }
  290. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  291. [dic setObject:defUser.userDict[@"school"] forKey:@"schoolId"];
  292. if (_terminalType) {
  293. [dic setObject:_terminalType forKey:@"type"];
  294. }
  295. NSString *method = @"devs";
  296. [MBProgressHUD showLoadToView:self.view];
  297. [NetManager requestAnythingWithURL:method dictionary:dic dataArray:nil completion:^(NSDictionary *root) {
  298. [MBProgressHUD hideHUDForView:self.view];
  299. if (!root) {
  300. ShowMsg(@"数据请求失败,请重试");
  301. return;
  302. }
  303. if ([root[@"code"] integerValue] == 1) {
  304. ShowMsg(root[@"msg"]);
  305. return;
  306. }
  307. dataArray = root[@"body"];
  308. if (dataArray == 0) {
  309. [_locService startUpdatingLocation];
  310. return;
  311. }
  312. [self createRightBtn];
  313. [self addPointAnnotationsWithArray:dataArray];
  314. }];
  315. }
  316. - (BMKUserLocation *)userLocation {
  317. if (!_userLocation) {
  318. _userLocation = [[BMKUserLocation alloc] init];
  319. }
  320. return _userLocation;
  321. }
  322. @end