MapVC.m 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. //
  2. // MapVC.m
  3. // jiaPei
  4. //
  5. // Created by apple on 16/2/18.
  6. // Copyright © 2016年 JCZ. All rights reserved.
  7. //
  8. #import "MapVC.h"
  9. #import "NeighbourBMKPointAnnotation.h"
  10. #import "NeighbourBMKAnnotationView.h"
  11. #import <BaiduMapAPI_Map/BMKMapComponent.h>//引入地图功能所有的头文件
  12. #import <BaiduMapAPI_Search/BMKSearchComponent.h>//引入检索功能所有的头文件
  13. #import <BMKLocationkit/BMKLocationComponent.h>//引入定位功能所有的头文件(新)
  14. #import <BaiduMapAPI_Utils/BMKUtilsComponent.h>
  15. @interface MapVC ()<BMKMapViewDelegate,BMKLocationManagerDelegate,BMKGeoCodeSearchDelegate>
  16. {
  17. BMKMapView *_mapView;
  18. CLLocation *meLocation;
  19. CLLocationCoordinate2D schLocation;
  20. }
  21. @property (nonatomic, strong) BMKUserLocation *userLocation;
  22. @end
  23. @implementation MapVC
  24. - (void)viewDidLoad {
  25. [super viewDidLoad];
  26. self.title = @"地图";
  27. self.view.backgroundColor = backGroundColor;
  28. [self configNavigationBar];
  29. [self myInit];
  30. }
  31. -(void)viewWillAppear:(BOOL)animated
  32. {
  33. [super viewWillAppear:animated];
  34. [_mapView viewWillAppear];
  35. _mapView.delegate = self; // 此处记得不用的时候需要置nil,否则影响内存的释放
  36. }
  37. -(void)viewWillDisappear:(BOOL)animated
  38. {
  39. [super viewWillDisappear:animated];
  40. [_mapView viewWillDisappear];
  41. _mapView.delegate = nil; // 不用时,置nil
  42. }
  43. -(void)myInit
  44. {
  45. _mapView = [[BMKMapView alloc]initWithFrame:[UIScreen mainScreen].bounds];
  46. _mapView.mapType = BMKMapTypeStandard;
  47. //定位功能
  48. [[MapManager sharedManager] updateLocationWithCompleteBlock:^(BOOL success, CLLocation * _Nonnull location) {
  49. if (success) {
  50. self.userLocation.location = location;
  51. [self->_mapView updateLocationData:self->_userLocation];
  52. self->meLocation = location;
  53. }
  54. }];
  55. //这个设置重复是为了消除一个bug 记得
  56. _mapView.showsUserLocation = NO;
  57. /*
  58. BMKUserTrackingModeNone 普通定位模式
  59. BMKUserTrackingModeFollow 定位跟随模式
  60. BMKUserTrackingModeFollowWithHeading 定位罗盘模式
  61. */
  62. _mapView.userTrackingMode = BMKUserTrackingModeNone;
  63. _mapView.zoomLevel = 17;
  64. _mapView.showsUserLocation = YES;
  65. BMKLocationViewDisplayParam *displayParam = [[BMKLocationViewDisplayParam alloc]init];
  66. displayParam.isRotateAngleValid = true;//跟随态旋转角度是否生效
  67. displayParam.isAccuracyCircleShow = false;//精度圈是否显示
  68. //displayParam.locationViewImgName= @"icon";//定位图标名称
  69. displayParam.locationViewOffsetX = 0;//定位偏移量(经度)
  70. displayParam.locationViewOffsetY = 0;//定位偏移量(纬度)
  71. [_mapView updateLocationViewWithParam:displayParam];
  72. [self.view addSubview:_mapView];
  73. if (_isJustLoad) {
  74. NeighbourBMKPointAnnotation *annotation = [[NeighbourBMKPointAnnotation alloc]init];
  75. CLLocationCoordinate2D coor;
  76. coor.latitude = _lat;
  77. coor.longitude = _lng;
  78. annotation.coordinate = coor;
  79. annotation.title = _siteName;
  80. _mapView.delegate = self;
  81. [_mapView addAnnotation:annotation];
  82. _mapView.centerCoordinate = coor;
  83. [_mapView selectAnnotation:annotation animated:NO];
  84. return;
  85. }
  86. if (!self.jxdz) {
  87. //如果驾校没地址就显示自己位置
  88. _mapView.userTrackingMode = BMKUserTrackingModeFollow;
  89. return;
  90. }
  91. //驾校位置地理反编码
  92. if (self.jxdz.length > 0) {
  93. NSMutableString* mstr = [NSMutableString stringWithString:self.jxdz];
  94. NSRange substr = [mstr rangeOfString:@"省"];
  95. NSRange substr2 = [mstr rangeOfString:@"市"];
  96. NSString *cityStr,*addressStr;
  97. if (substr2.location == NSNotFound)
  98. {
  99. cityStr = @"";
  100. addressStr = mstr;
  101. }
  102. else
  103. {
  104. if (substr.location == NSNotFound)
  105. {
  106. cityStr = [mstr substringWithRange:NSMakeRange(0, substr2.location + 1)];
  107. }
  108. else
  109. {
  110. cityStr = [mstr substringWithRange:NSMakeRange(substr.location + substr.length, substr2.location - substr.location - substr.length + 1)];
  111. }
  112. addressStr = [mstr substringFromIndex:substr2.location + 1];
  113. }
  114. [[MapManager sharedManager] onGetGeoCodeWithAddress:addressStr City:cityStr completeBlock:^(BOOL success, BMKGeoCodeSearchResult * _Nonnull result, BMKSearchErrorCode error) {
  115. if (success) {
  116. if (error == BMK_SEARCH_NO_ERROR) {
  117. //在此处理正常结果
  118. NeighbourBMKPointAnnotation* annotation = [[NeighbourBMKPointAnnotation alloc]init];
  119. ;
  120. annotation.coordinate = result.location;
  121. annotation.title = self.jxdz;
  122. [self->_mapView addAnnotation:annotation];
  123. self->_mapView.centerCoordinate = result.location;
  124. self->schLocation = result.location;
  125. //[self getDistance];
  126. }
  127. else {
  128. //如果没找到驾校位置 就找自己的位置
  129. self->_mapView.userTrackingMode = BMKUserTrackingModeFollow;
  130. }
  131. }
  132. }];
  133. }
  134. }
  135. //标注的代理
  136. -(BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id<BMKAnnotation>)annotation
  137. {
  138. if ([annotation isKindOfClass:[NeighbourBMKPointAnnotation class]]) {
  139. NeighbourBMKAnnotationView *view = [NeighbourBMKAnnotationView annotationViewWithMapView:mapView Annotation:(BMKPointAnnotation *)annotation];
  140. view.size = 1;
  141. view.block = ^(NeighbourBMKPointAnnotation *clickedAnnotation) {
  142. BMKOpenDrivingRouteOption *opt = [[BMKOpenDrivingRouteOption alloc] init];
  143. opt.appScheme = @"baidumapsdk://mapsdk.baidu.com";
  144. opt.isSupportWeb = YES;//调起百度地图客户端失败后,是否支持调起web地图,默认:YES
  145. //指定起点
  146. BMKPlanNode *start = [[BMKPlanNode alloc]init];
  147. start.pt = self->meLocation.coordinate;
  148. opt.startPoint = start;
  149. //指定终点名称
  150. BMKPlanNode *end = [[BMKPlanNode alloc]init];
  151. end.pt = CLLocationCoordinate2DMake(clickedAnnotation.coordinate.latitude, clickedAnnotation.coordinate.longitude);
  152. opt.endPoint = end;
  153. BMKOpenErrorCode code = [BMKOpenRoute openBaiduMapDrivingRoute:opt];
  154. if (code == 1 || code == 0) {
  155. }else{
  156. ShowMsg(@"打开百度地图失败");
  157. }
  158. };
  159. return view;
  160. }
  161. return nil;
  162. }
  163. //-(CGFloat)getDistance
  164. //{
  165. // CLLocation* dist=[[CLLocation alloc] initWithLatitude:schLocation.latitude longitude:schLocation.longitude];
  166. // CLLocationDistance kilometers=[meLocation distanceFromLocation:dist]/1000;
  167. //
  168. // NSLog(@"距离-->%f",kilometers);
  169. // return kilometers;
  170. //}
  171. - (void)didReceiveMemoryWarning {
  172. [super didReceiveMemoryWarning];
  173. // Dispose of any resources that can be recreated.
  174. }
  175. - (BMKUserLocation *)userLocation {
  176. if (!_userLocation) {
  177. _userLocation = [[BMKUserLocation alloc] init];
  178. }
  179. return _userLocation;
  180. }
  181. @end