LocationManager.m 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603
  1. //
  2. // LocationManager.m
  3. // jiaPei
  4. //
  5. // Created by 张嵘 on 2018/9/20.
  6. // Copyright © 2018年 JCZ. All rights reserved.
  7. //
  8. #import "LocationManager.h"
  9. #import <CoreLocation/CoreLocation.h>
  10. static LocationManager *manger = nil;
  11. static dispatch_once_t onceToken;
  12. //复用annotationView的指定唯一标识
  13. static NSString *annotationViewIdentifier = @"com.Baidu.BMKPointAnnotation";
  14. @interface LocationManager () <BMKLocationAuthDelegate, BMKLocationManagerDelegate, BMKMapViewDelegate, BMKGeneralDelegate, BMKGeoCodeSearchDelegate, BMKRecommendStopSearchDelegate>
  15. @property (nonatomic, strong) BMKMapManager *mapManager; //主引擎类
  16. @property (nonatomic, strong) BMKLocationManager *locationManager; //定位对象
  17. @property (nonatomic, strong) BMKUserLocation *userLocation;//当前位置对象
  18. @property (nonatomic, assign) BOOL locationLock;//单次定位锁
  19. @property (nonatomic, strong) LocCompletedBlock locCompletedBlock;//定位回调
  20. @property (nonatomic, strong) BMKMapView *mapView;//地图View类
  21. @property (nonatomic, assign) CGSize mapSize;//地图大小
  22. @property (nonatomic, strong) UIImageView *locationView;
  23. @property (nonatomic, strong) UIButton *addressBtn;
  24. @property (nonatomic, copy) NSArray<id<BMKAnnotation>> *annotationArr;
  25. @property (nonatomic, strong) BMKGeoCodeSearch *searcher;//搜索对象
  26. @property (nonatomic, strong) SearchCompletedBlock searchCompletedBlock;//搜索回调
  27. @property (nonatomic, strong) ReverseCompletedBlock reverseCompletedBlock;//反地理编码回调
  28. @property (nonatomic, strong) CLLocationManager *cLLocationManager;
  29. @end
  30. @implementation LocationManager
  31. + (LocationManager *)sharedManager {
  32. dispatch_once(&onceToken, ^{
  33. manger = [[self alloc] init];
  34. });
  35. return manger;
  36. }
  37. - (instancetype)init {
  38. self = [super init];
  39. if (self) {
  40. @weakify(self)
  41. dispatch_async(dispatch_get_main_queue(), ^{
  42. @strongify(self)
  43. // 初始化定位SDK
  44. [[BMKLocationAuth sharedInstance] checkPermisionWithKey:BaiDuMapAK authDelegate:self];
  45. [BMKMapManager setAgreePrivacy:YES];
  46. //要使用百度地图,请先启动BMKMapManager
  47. self.mapManager = [[BMKMapManager alloc] init];
  48. // ‼️重要:设置用户是否同意SDK隐私协议,请务必在BMKLocationManager和BMKGeoFenceManager实例化之前调用,否则实例化失败
  49. [[BMKLocationAuth sharedInstance] setAgreePrivacy:YES];
  50. /**
  51. 百度地图SDK所有API均支持百度坐标(BD09)和国测局坐标(GCJ02),用此方法设置您使用的坐标类型.
  52. 默认是BD09(BMK_COORDTYPE_BD09LL)坐标.
  53. 如果需要使用GCJ02坐标,需要设置CoordinateType为:BMK_COORDTYPE_COMMON.
  54. */
  55. if ([BMKMapManager setCoordinateTypeUsedInBaiduMapSDK:BMK_COORDTYPE_GPS]) {
  56. NSLog(@"经纬度类型设置成功");
  57. } else {
  58. NSLog(@"经纬度类型设置失败");
  59. }
  60. //启动引擎并设置AK并设置delegate
  61. BOOL result = [self.mapManager start:BaiDuMapAK generalDelegate:self];
  62. if (!result) {
  63. NSLog(@"启动引擎失败");
  64. }
  65. self.cLLocationManager = [[CLLocationManager alloc] init];
  66. [self.cLLocationManager requestWhenInUseAuthorization];
  67. });
  68. }
  69. return self;
  70. }
  71. - (void)dealloc {
  72. _mapView.delegate = nil;
  73. }
  74. #pragma mark - Custom Way
  75. /**
  76. 根据大小创建地图
  77. @param size 大小
  78. @return 返回地图
  79. */
  80. - (BMKMapView *)getMapViewWithSize:(CGSize)size {
  81. self.mapView = nil;
  82. _mapSize = size;
  83. [self.mapView addSubview:self.locationView];
  84. return self.mapView;
  85. }
  86. /**
  87. 单次定位
  88. @param completeBlock 定位回调
  89. */
  90. - (void)updateLocationWithCompleteBlock:(LocCompletedBlock)completeBlock {
  91. @weakify(self)
  92. self.locationLock = YES;
  93. _locCompletedBlock = completeBlock;
  94. self.locationManager.distanceFilter = kCLDistanceFilterNone;
  95. [self.locationManager requestLocationWithReGeocode:YES withNetworkState:YES completionBlock:^(BMKLocation * _Nullable location, BMKLocationNetworkState state, NSError * _Nullable error) {
  96. @strongify(self)
  97. if (_locCompletedBlock) {
  98. _locCompletedBlock(location != nil, location.location, location);
  99. }
  100. if (error) [self showErrorAlertWithError:error];
  101. }];
  102. }
  103. /**
  104. 连续定位
  105. @param distanceFilter 设定定位的最小更新距离。默认为 kCLDistanceFilterNone
  106. @param completeBlock 定位回调
  107. */
  108. - (void)updateLocationWithDistanceFilter:(NSInteger)distanceFilter CompleteBlock:(LocCompletedBlock)completeBlock {
  109. [self.locationManager startUpdatingLocation];
  110. self.locationManager.distanceFilter = distanceFilter? distanceFilter : kCLDistanceFilterNone;
  111. _locCompletedBlock = completeBlock;
  112. }
  113. /**
  114. 返回地址信息搜索结果
  115. @param address 地址(必填)
  116. @param city 城市(选填)
  117. @param completeBlock 搜索回调
  118. */
  119. - (void)onGetGeoCodeWithAddress:(NSString *)address City:(NSString *)city completeBlock:(SearchCompletedBlock)completeBlock {
  120. BMKGeoCodeSearchOption *geoCodeSearchOption = [[BMKGeoCodeSearchOption alloc]init];
  121. geoCodeSearchOption.city= city;
  122. geoCodeSearchOption.address = address;
  123. [self.searcher geoCode:geoCodeSearchOption];
  124. _searchCompletedBlock = completeBlock;
  125. }
  126. /**
  127. 返回反地理编码搜索结果
  128. @param location 定位
  129. @param completeBlock 搜索回调
  130. */
  131. - (void)onGetReverseGeoCodeWithLocation:(CLLocation *)location completeBlock:(ReverseCompletedBlock)completeBlock {
  132. BMKReverseGeoCodeSearchOption *reverseGeoCodeSearchOption = [[BMKReverseGeoCodeSearchOption alloc]init];
  133. reverseGeoCodeSearchOption.location = location.coordinate;
  134. [self.searcher reverseGeoCode:reverseGeoCodeSearchOption];
  135. _reverseCompletedBlock = completeBlock;
  136. }
  137. /**
  138. 错误弹窗
  139. */
  140. - (void)showErrorAlertWithError:(NSError *)error {
  141. switch (error.code) {
  142. case BMKLocationErrorDenied: {
  143. if (!RQ_AD_MANAGER.isFirstAppLoad) {
  144. [RQ_SHARE_FUNCTION showAlertWithTitle:@"温馨提示" message:@"请在iPhone的“设置”-“隐私”-“定位服务”功能中,找到“极速驾培”打开位置访问权限" alertControllerStyle:UIAlertControllerStyleAlert cancelButtonTitle:@"确定" otherButtonTitles:nil otherButtonStyles:nil showInWindow:NO completion:nil];
  145. }
  146. }
  147. break;
  148. default:
  149. break;
  150. }
  151. [_locationManager stopUpdatingLocation];
  152. }
  153. /**
  154. 开始定位
  155. */
  156. - (void)beginLocation{
  157. [self.locationManager startUpdatingLocation];
  158. [self.locationManager startUpdatingHeading];
  159. }
  160. #pragma mark - Search Data
  161. - (void)setupDefaultDataWithCLLocationCoordinate2D:(CLLocationCoordinate2D)locationCoordinate2D {
  162. // 初始化请求参数类BMKRecommendStopSearchOption的实例
  163. BMKRecommendStopSearchOption *option = [[BMKRecommendStopSearchOption alloc] init];
  164. // 推荐上车点经纬度 (必选)
  165. option.location = locationCoordinate2D;
  166. [self searchData:option];
  167. }
  168. - (void)searchData:(BMKRecommendStopSearchOption *)option {
  169. // 初始化BMKRecommendStopSearch实例
  170. BMKRecommendStopSearch *search = [[BMKRecommendStopSearch alloc] init];
  171. // 推荐上车点检索的代理
  172. search.delegate = self;
  173. // 初始化请求参数类BMKRecommendStopSearchOption的实例
  174. BMKRecommendStopSearchOption *stopSearchOption = [[BMKRecommendStopSearchOption alloc] init];
  175. // 推荐上车点经纬度 (必选)
  176. stopSearchOption.location = option.location;
  177. /// 推荐上车点检索
  178. /// @param recommendStopOption 推荐上车点检索信息类
  179. /// @return 成功返回YES,否则返回NO
  180. // BOOL flag = [search recommendStopSearch:stopSearchOption];
  181. // if (flag) {
  182. // NSLog(@"推荐上车点检索成功");
  183. // } else {
  184. // NSLog(@"推荐上车点检索失败");
  185. // }
  186. }
  187. #pragma mark - BMKLocationAuthDelegate 定位鉴权代理
  188. /**
  189. * @brief 返回授权验证错误
  190. * @param iError 错误号 : 为0时验证通过,具体参加BMKLocationAuthErrorCode
  191. */
  192. - (void)onCheckPermissionState:(BMKLocationAuthErrorCode)iError {
  193. if (_locCompletedBlock) {
  194. _locCompletedBlock(NO, nil, nil);
  195. }
  196. switch (iError) {
  197. case BMKLocationAuthErrorUnknown:
  198. NSLog(@"未知错误");
  199. break;
  200. case BMKLocationAuthErrorSuccess:
  201. NSLog(@"鉴权成功");
  202. break;
  203. case BMKLocationAuthErrorNetworkFailed:
  204. NSLog(@"因网络鉴权失败");
  205. break;
  206. case BMKLocationAuthErrorFailed:
  207. NSLog(@"KEY非法鉴权失败");
  208. break;
  209. default:
  210. break;
  211. }
  212. }
  213. /**
  214. 联网结果回调
  215. @param iError 联网结果错误码信息,0代表联网成功
  216. */
  217. - (void)onGetNetworkState:(int)iError {
  218. if (0 == iError) {
  219. NSLog(@"联网成功");
  220. } else {
  221. NSLog(@"联网失败:%d", iError);
  222. if (_searchCompletedBlock) {
  223. _searchCompletedBlock(NO, nil, iError);
  224. }
  225. if (_reverseCompletedBlock) {
  226. _reverseCompletedBlock(NO, nil, iError);
  227. }
  228. }
  229. }
  230. #pragma mark - BMKMapViewDelegate 地图代理
  231. - (void)mapView:(BMKMapView *)mapView regionDidChangeAnimated:(BOOL)animated {
  232. NSLog(@"地图拖动");
  233. [UIView animateWithDuration:0.30 animations:^{
  234. self.locationView.centerY -=8;
  235. } completion:^(BOOL finished) {
  236. self.locationView.centerY +=8;
  237. }];
  238. CGPoint touchPoint = self.mapView.center;
  239. CLLocationCoordinate2D touchMapCoordinate = [self.mapView convertPoint:touchPoint toCoordinateFromView:self.mapView];//这里touchMapCoordinate就是该点的经纬度了
  240. NSLog(@"touching %f,%f",touchMapCoordinate.latitude,touchMapCoordinate.longitude);
  241. [self setupDefaultDataWithCLLocationCoordinate2D:touchMapCoordinate];
  242. BMKReverseGeoCodeSearchOption *reverseGeoCodeSearchOption = [[BMKReverseGeoCodeSearchOption alloc]init];
  243. reverseGeoCodeSearchOption.location = touchMapCoordinate;
  244. [self.searcher reverseGeoCode:reverseGeoCodeSearchOption];
  245. }
  246. /// 根据anntation生成对应的View
  247. /// @param mapView 地图View
  248. /// @param annotation 指定的标注
  249. /// @return 生成的标注View
  250. - (__kindof BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id<BMKAnnotation>)annotation {
  251. if ([annotation isKindOfClass:[BMKPointAnnotation class]]) {
  252. /**
  253. 根据指定标识查找一个可被复用的标注,用此方法来代替新创建一个标注,返回可被复用的标注
  254. */
  255. BMKPinAnnotationView *annotationView = (BMKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:annotationViewIdentifier];
  256. UILabel *addressLabel = [UILabel rq_labelWithText:annotation.title font:RQRegularFont_14 textColor:RQColorFromHexString(@"#498EF5")];
  257. addressLabel.numberOfLines = 0;
  258. if (!annotationView) {
  259. /**
  260. 初始化并返回一个annotationView
  261. @param annotation 关联的annotation对象
  262. @param reuseIdentifier 如果要重用view,传入一个字符串,否则设为nil,建议重用view
  263. @return 初始化成功则返回annotationView,否则返回nil
  264. */
  265. annotationView = [[BMKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:annotationViewIdentifier];
  266. //annotationView显示的图片,默认是大头针
  267. // annotationView.image = nil;
  268. /**
  269. 默认情况下annotationView的中心点位于annotation的坐标位置,可以设置centerOffset改变
  270. annotationView的位置,正的偏移使annotationView朝右下方移动,负的朝左上方,单位是像素
  271. */
  272. annotationView.centerOffset = CGPointMake(0, 0);
  273. /**
  274. 默认情况下, 弹出的气泡位于annotationView正中上方,可以设置calloutOffset改变annotationView的
  275. 位置,正的偏移使annotationView朝右下方移动,负的朝左上方,单位是像素
  276. */
  277. annotationView.calloutOffset = CGPointMake(0, 0);
  278. //是否显示3D效果,标注在地图旋转和俯视时跟随旋转、俯视,默认为NO
  279. annotationView.enabled3D = NO;
  280. //是否忽略触摸时间,默认为YES
  281. annotationView.enabled = YES;
  282. /**
  283. 开发者不要直接设置这个属性,若设置,需要在设置后调用BMKMapView的-(void)mapForceRefresh;方法
  284. 刷新地图,默认为NO,当annotationView被选中时为YES
  285. */
  286. annotationView.selected = NO;
  287. //annotationView被选中时,是否显示气泡(若显示,annotation必须设置了title),默认为YES
  288. annotationView.canShowCallout = NO;
  289. /**
  290. 显示在气泡左侧的view(使用默认气泡时,view的width最大值为32,
  291. height最大值为41,大于则使用最大值)
  292. */
  293. annotationView.leftCalloutAccessoryView = nil;
  294. /**
  295. 显示在气泡右侧的view(使用默认气泡时,view的width最大值为32,
  296. height最大值为41,大于则使用最大值)
  297. */
  298. annotationView.rightCalloutAccessoryView = nil;
  299. /**
  300. annotationView的颜色: BMKPinAnnotationColorRed,BMKPinAnnotationColorGreen,
  301. BMKPinAnnotationColorPurple
  302. */
  303. annotationView.pinColor = BMKPinAnnotationColorGreen;
  304. annotationView.displayMinLevel = 17;
  305. //设置从天而降的动画效果
  306. annotationView.animatesDrop = NO;
  307. //当设为YES并实现了setCoordinate:方法时,支持将view在地图上拖动
  308. annotationView.draggable = NO;
  309. //当前view的拖动状态
  310. //annotationView.dragState;
  311. [annotationView addSubview:addressLabel];
  312. [addressLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
  313. make.left.mas_equalTo(annotationView.mas_right).mas_offset(8);
  314. make.centerY.mas_equalTo(annotationView);
  315. make.size.mas_equalTo(CGSizeMake(RQ_SCREEN_WIDTH / 3.f, 50.f));
  316. }];
  317. } else {
  318. [annotationView removeAllSubviews];
  319. [annotationView addSubview:addressLabel];
  320. [addressLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
  321. make.left.mas_equalTo(annotationView.mas_right).mas_offset(8);
  322. make.centerY.mas_equalTo(annotationView);
  323. make.size.mas_equalTo(CGSizeMake(RQ_SCREEN_WIDTH / 3.f, 50.f));
  324. }];
  325. }
  326. return annotationView;
  327. }
  328. return nil;
  329. }
  330. #pragma mark - BMKGeoCodeSearchDelegate 搜索代理
  331. - (void)onGetGeoCodeResult:(BMKGeoCodeSearch *)searcher result:(BMKGeoCodeSearchResult *)result errorCode:(BMKSearchErrorCode)error {
  332. if (_searchCompletedBlock) {
  333. _searchCompletedBlock(result? YES : NO, result, error);
  334. }
  335. }
  336. - (void)onGetReverseGeoCodeResult:(BMKGeoCodeSearch *)searcher result:(BMKReverseGeoCodeSearchResult *)result errorCode:(BMKSearchErrorCode)error {
  337. if (result) {
  338. if (result.poiRegions.count > 0) {
  339. BMKSearchRGCRegionInfo *info = result.poiRegions.firstObject;
  340. [self.addressBtn setTitleNormal:info.regionName];
  341. } else {
  342. [self.addressBtn setTitleNormal:result.sematicDescription];
  343. }
  344. }
  345. if (_reverseCompletedBlock) {
  346. _reverseCompletedBlock(result ? YES : NO, result, error);
  347. }
  348. }
  349. #pragma mark - BMKRecommendStopSearchDelegate
  350. /// 推荐上车点检索结果回调
  351. /// @param searcher 搜索对象
  352. /// @param recommendStopResult 搜索结果
  353. /// @param errorCode 错误号,@see BMKSearchErrorCode
  354. - (void)onGetRecommendStopResult:(BMKRecommendStopSearch *)searcher result:(BMKRecommendStopSearchResult *)recommendStopResult errorCode:(BMKSearchErrorCode)errorCode {
  355. if (errorCode == BMK_SEARCH_NO_ERROR) {
  356. if (!recommendStopResult.recommendStopInfoList && recommendStopResult.recommendStopInfoList.count == 0) {
  357. return;
  358. }
  359. [_mapView removeAnnotations:self.annotationArr];
  360. self.annotationArr = [recommendStopResult.recommendStopInfoList.rac_sequence.signal map:^id _Nullable(BMKRecommendStopInfo * _Nullable info) {
  361. BMKPointAnnotation *annotation = [[BMKPointAnnotation alloc] init];
  362. annotation.title = info.name;
  363. annotation.coordinate = info.location;
  364. return annotation;
  365. }].toArray;
  366. [_mapView addAnnotations:self.annotationArr];
  367. NSMutableArray *array = [NSMutableArray arrayWithArray:recommendStopResult.recommendStopInfoList];
  368. for (int i = 0; i< array.count -1; i++) {
  369. BOOL isEnd = NO;//判断是否已排序完成
  370. for (int j = 0; j < array.count -1 -i; j++) {
  371. BMKRecommendStopInfo *infoA = array[j];
  372. CGFloat a = infoA.distance;
  373. BMKRecommendStopInfo *infoB = array[j+1];
  374. CGFloat b = infoB.distance;
  375. if (a > b ) {
  376. isEnd = YES;
  377. [array exchangeObjectAtIndex:j withObjectAtIndex:j+1];
  378. }
  379. }
  380. if (!isEnd) {
  381. break;
  382. }
  383. }
  384. if (array.count > 0) {
  385. BMKRecommendStopInfo *info = array.firstObject;
  386. BMKPointAnnotation *annotation = [[BMKPointAnnotation alloc] init];
  387. annotation.title = info.name;
  388. annotation.coordinate = info.location;
  389. [_mapView selectAnnotation:annotation animated:YES];
  390. BMKMapStatus *status = [[BMKMapStatus alloc] init];
  391. status.targetGeoPt = info.location;
  392. status.fLevel = _mapView.zoomLevel;
  393. status.fRotation = _mapView.rotation;
  394. status.fOverlooking = _mapView.overlooking;
  395. [_mapView setMapStatus:status withAnimation:YES];
  396. }
  397. BMKRecommendStopInfo *info = [recommendStopResult.recommendStopInfoList firstObject];
  398. NSString *message = [NSString stringWithFormat:@"推荐上车点名称:%@\n推荐上车点地址:%@\n推荐点poi的uid:%@\n距查找点的距离:%.2lf\n经度:%.6lf\n纬度:%.6lf", info.name, info.address, info.uid, info.distance, info.location.longitude, info.location.latitude];
  399. [jiaPeiManager QMLogStr:message];
  400. // [NSObject rq_showAlertViewWithTitle:@"推荐上车点" message:message confirmTitle:@"确定"];
  401. } else {
  402. // [_mapView removeAnnotations:self.annotationArr];
  403. }
  404. }
  405. #pragma mark - BMKLocationManagerDelegate 定位代理
  406. /**
  407. 用户拒绝开启定位服务等原因导致的定位失败会调用的方法
  408. @brief 当定位发生错误时,会调用代理的此方法。
  409. @param manager 定位 BMKLocationManager 类。
  410. @param error 返回的错误,参考 CLError 。
  411. */
  412. - (void)BMKLocationManager:(BMKLocationManager *)manager didFailWithError:(NSError *)error {
  413. if (error) [self showErrorAlertWithError:error];
  414. }
  415. /**
  416. 处理位置坐标更新
  417. @brief 连续定位回调函数
  418. @param manager 定位 BMKLocationManager 类。
  419. @param location 定位结果,参考BMKLocation。
  420. @param error 错误信息。
  421. */
  422. - (void)BMKLocationManager:(BMKLocationManager *)manager didUpdateLocation:(BMKLocation *)location orError:(NSError *)error {
  423. if (location) {
  424. self.userLocation.location = location.location;
  425. //实现该方法,否则定位图标不出现
  426. [_mapView updateLocationData:self.userLocation];
  427. //设置当前地图的中心点,改变该值时,地图的比例尺级别不会发生变化
  428. _mapView.centerCoordinate = self.userLocation.location.coordinate;
  429. if (_locCompletedBlock) {
  430. _locCompletedBlock(location != nil , location.location, location);
  431. }
  432. [_locationManager stopUpdatingLocation];
  433. [self setupDefaultDataWithCLLocationCoordinate2D:location.location.coordinate];
  434. //发起反向地理编码检索
  435. _searcher =[[BMKGeoCodeSearch alloc]init];
  436. _searcher.delegate = self;
  437. BMKReverseGeoCodeSearchOption *reverseGeoCodeSearchOption = [[BMKReverseGeoCodeSearchOption alloc]init];
  438. reverseGeoCodeSearchOption.location = location.location.coordinate;
  439. [_searcher reverseGeoCode:reverseGeoCodeSearchOption];
  440. }
  441. }
  442. #pragma mark - Lazy loading
  443. - (BMKLocationManager *)locationManager {
  444. if (!_locationManager) {
  445. _locationManager = [[BMKLocationManager alloc] init];
  446. _locationManager.delegate = self;
  447. _locationManager.coordinateType = BMKLocationCoordinateTypeBMK09LL;
  448. _locationManager.desiredAccuracy = kCLLocationAccuracyBest;
  449. _locationManager.activityType = CLActivityTypeAutomotiveNavigation;
  450. _locationManager.pausesLocationUpdatesAutomatically = NO;
  451. _locationManager.allowsBackgroundLocationUpdates = NO;
  452. _locationManager.locationTimeout = 10;
  453. }
  454. return _locationManager;
  455. }
  456. - (BMKUserLocation *)userLocation {
  457. if (!_userLocation) {
  458. _userLocation = [[BMKUserLocation alloc] init];
  459. }
  460. return _userLocation;
  461. }
  462. - (BMKMapView *)mapView {
  463. if (!_mapView) {
  464. _mapView = [[BMKMapView alloc] initWithFrame:CGRectMake(0,0,_mapSize.width, _mapSize.height)];
  465. //开启定位服务
  466. [self beginLocation];
  467. //设置mapView的代理
  468. _mapView.delegate = self;
  469. //当前地图类型
  470. _mapView.mapType = BMKMapTypeStandard;
  471. //设置地图比例尺级别
  472. _mapView.zoomLevel = 19;
  473. //设置显示定位图层
  474. _mapView.showsUserLocation = YES;
  475. //设置定位模式为普通模式
  476. _mapView.userTrackingMode = BMKUserTrackingModeHeading;
  477. //设定显式比例尺
  478. // _mapView.showMapScaleBar = YES;
  479. // 关闭旋转
  480. _mapView.rotateEnabled = NO;
  481. _mapView.ChangeCenterWithDoubleTouchPointEnabled = NO;
  482. //配置定位图层个性化样式,初始化BMKLocationViewDisplayParam的实例
  483. BMKLocationViewDisplayParam *param = [[BMKLocationViewDisplayParam alloc] init];
  484. //设置定位图标(屏幕坐标)X轴偏移量为0
  485. param.locationViewOffsetX = 0;
  486. //设置定位图标(屏幕坐标)Y轴偏移量为0
  487. param.locationViewOffsetY = 0;
  488. //设置定位图层locationView在最上层(也可设置为在下层)
  489. param.locationViewHierarchy = LOCATION_VIEW_HIERARCHY_TOP;
  490. //设置显示精度圈(因“需求:可改变精度圈大小”,关闭默认精度圈)
  491. param.isAccuracyCircleShow = YES;
  492. //跟随态旋转角度是否生效
  493. param.isRotateAngleValid = YES;
  494. //更新定位图层个性化样式
  495. [_mapView updateLocationViewWithParam:param];
  496. }
  497. return _mapView;
  498. }
  499. - (BMKGeoCodeSearch *)searcher {
  500. if (!_searcher) {
  501. _searcher =[[BMKGeoCodeSearch alloc]init];
  502. _searcher.delegate = self;
  503. }
  504. return _searcher;
  505. }
  506. - (UIImageView *)locationView {
  507. if (!_locationView) {
  508. _locationView =[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"定位针"]];
  509. _locationView.center = CGPointMake(self.mapView.width/2, self.mapView.height/2);
  510. [_locationView addSubview:self.addressBtn];
  511. @weakify(self)
  512. [self.addressBtn mas_makeConstraints:^(MASConstraintMaker *make) {
  513. @strongify(self)
  514. make.bottom.mas_equalTo(_locationView.top).mas_offset(- _locationView.rq_height / 2.f);
  515. make.centerX.mas_equalTo(_locationView);
  516. make.size.mas_equalTo(CGSizeMake(RQ_SCREEN_WIDTH / 3.f, 54.f));
  517. self.addressBtn.layer.cornerRadius = 27.f;
  518. }];
  519. }
  520. return _locationView;
  521. }
  522. - (UIButton *)addressBtn {
  523. if (!_addressBtn) {
  524. _addressBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  525. _addressBtn.titleLabel.numberOfLines = 0;
  526. _addressBtn.titleLabel.font = RQRegularFont_14;
  527. [_addressBtn setBackgroundColor:RQColorFromHexString(@"#232323")];
  528. [_addressBtn setTitleColor:RQColorFromHexString(@"#498EF5") forState:UIControlStateNormal];
  529. }
  530. return _addressBtn;
  531. }
  532. @end