DistenceTooLongMapVC.m 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. //
  2. // DistenceTooLongMapVC.m
  3. // jiaPei
  4. //
  5. // Created by apple on 2018/1/31.
  6. // Copyright © 2018年 JCZ. All rights reserved.
  7. //
  8. #import "DistenceTooLongMapVC.h"
  9. #import <BaiduMapAPI_Map/BMKMapComponent.h>//引入地图功能所有的头文件
  10. @interface DistenceTooLongMapVC ()<BMKMapViewDelegate>
  11. {
  12. BMKMapView *_mapView;
  13. }
  14. @end
  15. @implementation DistenceTooLongMapVC
  16. - (void)viewDidLoad {
  17. [super viewDidLoad];
  18. self.title = @"扫码位置";
  19. self.view.backgroundColor = backGroundColor;
  20. [self configNavigationBar];
  21. [self myInit];
  22. }
  23. -(void)viewWillAppear:(BOOL)animated
  24. {
  25. [super viewWillAppear:animated];
  26. [_mapView viewWillAppear];
  27. _mapView.delegate = self; // 此处记得不用的时候需要置nil,否则影响内存的释放
  28. }
  29. -(void)viewWillDisappear:(BOOL)animated
  30. {
  31. [super viewWillDisappear:animated];
  32. [_mapView viewWillDisappear];
  33. _mapView.delegate = nil; // 不用时,置nil
  34. }
  35. - (void)myInit {
  36. _mapView = [[BMKMapView alloc]initWithFrame:CGRectMake(0, 0, kSize.width, kSize.height*7/12.0)];
  37. _mapView.delegate = self;
  38. [self.view addSubview:_mapView];
  39. NSMutableArray *annotationArray = [NSMutableArray arrayWithCapacity:2];
  40. BMKPointAnnotation *annotation = [[BMKPointAnnotation alloc]init];
  41. annotation.coordinate = CLLocationCoordinate2DMake(_lat1, _lng1);
  42. annotation.title = @"当前位置";
  43. annotation.subtitle = [NSString stringWithFormat:@"距离教室%@米",_distence];
  44. [_mapView addAnnotation:annotation];
  45. [annotationArray addObject:annotation];
  46. annotation = [[BMKPointAnnotation alloc]init];
  47. annotation.coordinate = CLLocationCoordinate2DMake(_lat2, _lng2);
  48. annotation.title = @"理论教室位置";
  49. annotation.subtitle = [NSString stringWithFormat:@"距离您%@米",_distence];
  50. [_mapView addAnnotation:annotation];
  51. [annotationArray addObject:annotation];
  52. //中间点
  53. annotation = [[BMKPointAnnotation alloc]init];
  54. annotation.coordinate = CLLocationCoordinate2DMake(_lat1/2.0 + _lat2/2.0, _lng1/2.0 + _lng2/2.0);
  55. annotation.title = [NSString stringWithFormat:@"距离%.1fkm",[_distence floatValue]/1000.0];
  56. // annotation.subtitle = [NSString stringWithFormat:@"距离%.1fkm",[_distence floatValue]/1000.0];
  57. [_mapView addAnnotation:annotation];
  58. [annotationArray addObject:annotation];
  59. [_mapView selectAnnotation:annotation animated:YES];
  60. _mapView.centerCoordinate = annotation.coordinate;
  61. [_mapView showAnnotations:annotationArray animated:NO];
  62. _mapView.zoomLevel = _mapView.zoomLevel - 0.55;
  63. CLLocationCoordinate2D coor[2] = {0};
  64. coor[0] = CLLocationCoordinate2DMake(_lat1, _lng1);
  65. coor[1] = CLLocationCoordinate2DMake(_lat2, _lng2);
  66. BMKPolyline* polyline = [BMKPolyline polylineWithCoordinates:coor count:2];
  67. [_mapView addOverlay:polyline];
  68. //地图下UI
  69. CGFloat y = kSize.height * 7/12.0 - 24;//剩余24的距离用来掩盖百度的logo
  70. CGFloat h = kSize.height * 5/12.0 - 40;
  71. UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, y, kSize.width, h)];
  72. view.backgroundColor = backGroundColor;
  73. [self.view addSubview:view];
  74. [view addViewWithRect:CGRectMake(0, y, kSize.width, 1.0)];
  75. UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20, 5, kSize.width - 40, h - 60)];
  76. [label setText:@"说明:当前位置距离理论教室位置过远,请前往理论教室所在地扫描二维码签到。如果当前位置获取有误,已在理论教室,请点击“重新定位”重置当前位置后再次扫码签到,祝您学车愉快!" Font:FontTitle TextColor:[UIColor orangeColor]];
  77. label.numberOfLines = 0;
  78. [view addSubview:label];
  79. UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(20, h - 60, kSize.width - 40, 40)];
  80. btn.backgroundColor = defGreen;
  81. [btn setTitle:@"重新定位" textColor:[UIColor whiteColor] font:FontLarger fotState:UIControlStateNormal];
  82. [btn borderColor:backGroundColor width:.3 cornorRadios:4];
  83. [btn target:self tag:1];
  84. [view addSubview:btn];
  85. }
  86. - (void)btnClick:(UIButton *)sender {
  87. if (sender.tag == 1) {
  88. if (self.skipMspBlock) {
  89. self.skipMspBlock();
  90. }
  91. [self.navigationController popViewControllerAnimated:YES];
  92. }
  93. }
  94. //标注的代理
  95. -(BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id<BMKAnnotation>)annotation
  96. {
  97. if ([annotation isKindOfClass:[BMKPointAnnotation class]]) {
  98. BMKPinAnnotationView *newAnnotationView = [[BMKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"myAnnotation"];
  99. if ([annotation.title isEqualToString:@"当前位置"]) {
  100. newAnnotationView.image = [UIImage imageNamed:@"location_now"];
  101. }
  102. if ([annotation.title isEqualToString:@"理论教室位置"]) {
  103. newAnnotationView.image = [UIImage imageNamed:@"location_target"];
  104. }
  105. if ([annotation.title containsString:@"距离"]) {
  106. newAnnotationView.image = [UIImage imageNamed:@""];
  107. }
  108. // 设置该标注点动画显示
  109. newAnnotationView.animatesDrop = NO;
  110. return newAnnotationView;
  111. }
  112. return nil;
  113. }
  114. //根据overlay生成对应的View
  115. - (BMKOverlayView *)mapView:(BMKMapView *)mapView viewForOverlay:(id <BMKOverlay>)overlay
  116. {
  117. if ([overlay isKindOfClass:[BMKPolyline class]]) {
  118. BMKPolylineView *polylineView = [[BMKPolylineView alloc] initWithOverlay:overlay];
  119. polylineView.lineWidth = 1.6;
  120. polylineView.strokeColor = [UIColor redColor];
  121. return polylineView;
  122. }
  123. return nil;
  124. }
  125. - (void)didReceiveMemoryWarning {
  126. [super didReceiveMemoryWarning];
  127. }
  128. @end