MapVC.m 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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 <BaiduMapAPI_Map/BMKMapComponent.h>//引入地图功能所有的头文件
  10. #import <BaiduMapAPI_Search/BMKSearchComponent.h>//引入检索功能所有的头文件
  11. #import <BMKLocationkit/BMKLocationComponent.h>//引入定位功能所有的头文件(新)
  12. @interface MapVC ()<BMKMapViewDelegate,BMKLocationManagerDelegate,BMKGeoCodeSearchDelegate>
  13. {
  14. BMKMapView *_mapView;
  15. BMKLocationManager *_locService;
  16. BMKGeoCodeSearch *_searcher;
  17. CLLocation *meLocation;
  18. CLLocationCoordinate2D schLocation;
  19. }
  20. @property (nonatomic, strong) BMKUserLocation *userLocation;//当前位置对象
  21. @end
  22. @implementation MapVC
  23. - (void)viewDidLoad {
  24. [super viewDidLoad];
  25. self.title = @"地图";
  26. self.view.backgroundColor = KBackGroundColor;
  27. [self goBackByNavigation];
  28. [self myInit];
  29. }
  30. -(void)viewWillAppear:(BOOL)animated
  31. {
  32. [super viewWillAppear:animated];
  33. [_mapView viewWillAppear];
  34. _mapView.delegate = self; // 此处记得不用的时候需要置nil,否则影响内存的释放
  35. }
  36. -(void)viewWillDisappear:(BOOL)animated
  37. {
  38. [super viewWillDisappear:animated];
  39. [_mapView viewWillDisappear];
  40. _mapView.delegate = nil; // 不用时,置nil
  41. _searcher.delegate = nil;
  42. }
  43. -(void)myInit
  44. {
  45. _mapView = [[BMKMapView alloc]initWithFrame:[UIScreen mainScreen].bounds];
  46. _mapView.mapType = BMKMapTypeStandard;
  47. //定位功能
  48. _locService = [[BMKLocationManager alloc]init];
  49. _locService.delegate = self;
  50. [_locService startUpdatingLocation];
  51. //这个设置重复是为了消除一个bug 记得
  52. _mapView.showsUserLocation = NO;
  53. /*
  54. BMKUserTrackingModeNone 普通定位模式
  55. BMKUserTrackingModeFollow 定位跟随模式
  56. BMKUserTrackingModeFollowWithHeading 定位罗盘模式
  57. */
  58. _mapView.userTrackingMode = BMKUserTrackingModeNone;
  59. _mapView.zoomLevel = 17;
  60. _mapView.showsUserLocation = YES;
  61. BMKLocationViewDisplayParam *displayParam = [[BMKLocationViewDisplayParam alloc]init];
  62. displayParam.isRotateAngleValid = true;//跟随态旋转角度是否生效
  63. displayParam.isAccuracyCircleShow = false;//精度圈是否显示
  64. //displayParam.locationViewImgName= @"icon";//定位图标名称
  65. displayParam.locationViewOffsetX = 0;//定位偏移量(经度)
  66. displayParam.locationViewOffsetY = 0;//定位偏移量(纬度)
  67. [_mapView updateLocationViewWithParam:displayParam];
  68. [self.view addSubview:_mapView];
  69. if (_isJustLoad) {
  70. BMKPointAnnotation *annotation = [[BMKPointAnnotation alloc]init];
  71. CLLocationCoordinate2D coor;
  72. coor.latitude = _lat;
  73. coor.longitude = _lng;
  74. annotation.coordinate = coor;
  75. annotation.title = _siteName;
  76. [_mapView addAnnotation:annotation];
  77. _mapView.centerCoordinate = coor;
  78. [_mapView selectAnnotation:annotation animated:NO];
  79. return;
  80. }
  81. if (!self.jxdz) {
  82. //如果驾校没地址就显示自己位置
  83. _mapView.userTrackingMode = BMKUserTrackingModeFollow;
  84. return;
  85. }
  86. //驾校位置地理反编码
  87. if (self.jxdz.length > 0) {
  88. NSMutableString* mstr = [NSMutableString stringWithString:self.jxdz];
  89. NSRange substr = [mstr rangeOfString:@"省"];
  90. NSRange substr2 = [mstr rangeOfString:@"市"];
  91. NSString *cityStr,*addressStr;
  92. if (substr2.location == NSNotFound)
  93. {
  94. cityStr = @"";
  95. addressStr = mstr;
  96. }
  97. else
  98. {
  99. if (substr.location == NSNotFound)
  100. {
  101. cityStr = [mstr substringWithRange:NSMakeRange(0, substr2.location + 1)];
  102. }
  103. else
  104. {
  105. cityStr = [mstr substringWithRange:NSMakeRange(substr.location + substr.length, substr2.location - substr.location - substr.length + 1)];
  106. }
  107. addressStr = [mstr substringFromIndex:substr2.location + 1];
  108. }
  109. //NSLog(@"cityStr-->%@--addressStr-->%@",cityStr,addressStr);
  110. _searcher =[[BMKGeoCodeSearch alloc]init];
  111. _searcher.delegate = self;
  112. BMKGeoCodeSearchOption *geoCodeSearchOption = [[BMKGeoCodeSearchOption alloc]init];
  113. geoCodeSearchOption.city= cityStr;
  114. geoCodeSearchOption.address = addressStr;
  115. [_searcher geoCode:geoCodeSearchOption];
  116. }
  117. }
  118. - (void)onGetGeoCodeResult:(BMKGeoCodeSearch *)searcher result:(BMKGeoCodeSearchResult *)result errorCode:(BMKSearchErrorCode)error {
  119. if (error == BMK_SEARCH_NO_ERROR) {
  120. //在此处理正常结果
  121. BMKPointAnnotation* annotation = [[BMKPointAnnotation alloc]init];
  122. ;
  123. annotation.coordinate = result.location;
  124. annotation.title = self.jxdz;
  125. [_mapView addAnnotation:annotation];
  126. _mapView.centerCoordinate = result.location;
  127. schLocation = result.location;
  128. //[self getDistance];
  129. }
  130. else {
  131. //如果没找到驾校位置 就找自己的位置
  132. _mapView.userTrackingMode = BMKUserTrackingModeFollow;
  133. }
  134. }
  135. //标注的代理
  136. - (BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id<BMKAnnotation>)annotation {
  137. if ([annotation isKindOfClass:[BMKPointAnnotation class]]) {
  138. BMKPinAnnotationView *newAnnotationView = [[BMKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"myAnnotation"];
  139. newAnnotationView.pinColor = BMKPinAnnotationColorPurple;
  140. // 设置该标注点动画显示
  141. newAnnotationView.animatesDrop = YES;
  142. return newAnnotationView;
  143. }
  144. return nil;
  145. }
  146. //处理位置坐标更新
  147. - (void)BMKLocationManager:(BMKLocationManager *)manager didUpdateLocation:(BMKLocation *)location orError:(NSError *)error {
  148. self.userLocation.location = location.location;
  149. [_mapView updateLocationData:_userLocation];
  150. meLocation = _userLocation.location;
  151. }
  152. //-(CGFloat)getDistance
  153. //{
  154. // CLLocation* dist=[[CLLocation alloc] initWithLatitude:schLocation.latitude longitude:schLocation.longitude];
  155. // CLLocationDistance kilometers=[meLocation distanceFromLocation:dist]/1000;
  156. //
  157. // NSLog(@"距离-->%f",kilometers);
  158. // return kilometers;
  159. //}
  160. - (BMKUserLocation *)userLocation {
  161. if (!_userLocation) {
  162. _userLocation = [[BMKUserLocation alloc] init];
  163. }
  164. return _userLocation;
  165. }
  166. @end