LocServiceVC.m 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. //
  2. // LocServiceVC.m
  3. // LN_School
  4. //
  5. // Created by EchoShacolee on 2017/12/16.
  6. // Copyright © 2017年 Danson. All rights reserved.
  7. //
  8. #import "LocServiceVC.h"
  9. #import "LocSeriviceCell.h"
  10. #import <BaiduMapAPI_Map/BMKMapComponent.h>//引入地图功能所有的头文件
  11. #import <BMKLocationkit/BMKLocationComponent.h>//引入定位功能所有的头文件(新)
  12. #import <BaiduMapAPI_Search/BMKSearchComponent.h>//引入检索功能所有的头文件
  13. @interface LocServiceVC ()<BMKLocationManagerDelegate,BMKMapViewDelegate,BMKGeoCodeSearchDelegate,UITableViewDataSource,UITableViewDelegate>
  14. {
  15. CLLocationCoordinate2D _coordinate;
  16. BMKGeoCodeSearch *_searcher;
  17. BMKLocationManager *_locService;
  18. NSMutableArray *_poiInfos;
  19. }
  20. @property (nonatomic, strong) BMKUserLocation *userLocation;//当前位置对象
  21. @property(nonatomic,strong)BMKPointAnnotation * annotation;
  22. @end
  23. @implementation LocServiceVC
  24. -(BMKPointAnnotation *)annotation{
  25. if (!_annotation) {
  26. _annotation = [[BMKPointAnnotation alloc]init];
  27. }
  28. return _annotation;
  29. }
  30. - (void)viewDidLoad {
  31. [super viewDidLoad];
  32. self.navigationItem.title = @"位置";
  33. [self goBackByNavigation];
  34. [self customRightBtn];
  35. self.tableView.tableFooterView = [UIView new];
  36. _poiInfos = [NSMutableArray array];
  37. //定位功能
  38. _locService = [[BMKLocationManager alloc]init];
  39. _locService.distanceFilter = 10;//将被通知任何移动(若=10,表示超出10米才会回调位置信息)
  40. _locService.delegate = self;
  41. [_locService startUpdatingLocation];
  42. MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.tableView animated:YES];
  43. // hud.label.text = @"正在检索...";
  44. hud.backgroundColor = [UIColor whiteColor];
  45. hud.removeFromSuperViewOnHide = YES;
  46. }
  47. -(void)viewWillAppear:(BOOL)animated
  48. {
  49. [super viewWillAppear:animated];
  50. [_mapView viewWillAppear];
  51. _mapView.delegate = self; // 此处记得不用的时候需要置nil,否则影响内存的释放
  52. }
  53. -(void)viewWillDisappear:(BOOL)animated
  54. {
  55. [super viewWillDisappear:animated];
  56. [_mapView viewWillDisappear];
  57. _mapView.delegate = nil; // 不用时,置nil
  58. _searcher.delegate = nil;
  59. }
  60. -(void)customRightBtn{
  61. UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
  62. btn.frame = CGRectMake(0, 0, 43, 44);
  63. btn.titleLabel.font = [UIFont systemFontOfSize:17];
  64. [btn setTitle:@"上传" forState:UIControlStateNormal];
  65. [btn setTitleColor:defGreen forState:UIControlStateNormal];
  66. [btn addTarget:self action:@selector(rightBtnClick) forControlEvents:UIControlEventTouchUpInside];
  67. self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:btn];
  68. }
  69. - (void)rightBtnClick {
  70. if (!_coordinate.latitude) {
  71. ShowMsg(@"未选择位置");
  72. return;
  73. }
  74. if (self.block) {
  75. self.block(_coordinate.longitude, _coordinate.latitude);
  76. [self.navigationController popViewControllerAnimated:YES];
  77. }
  78. }
  79. #pragma mark 定位相关
  80. /**用户拒绝开启定位服务等原因导致的定位失败会调用的方法
  81. */
  82. - (void)BMKLocationManager:(BMKLocationManager *)manager didFailWithError:(NSError *)error {
  83. UIAlertView *alert =[[UIAlertView alloc]initWithTitle:@"温馨提示" message:@"未打开位置访问权限" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles: nil];//@"请在iPhone的“设置”-“隐私”-“定位服务”功能中,找到“优易学车”打开位置访问权限"
  84. [alert show];
  85. [_locService startUpdatingLocation];
  86. }
  87. //处理位置坐标更新
  88. - (void)BMKLocationManager:(BMKLocationManager *)manager didUpdateLocation:(BMKLocation *)location orError:(NSError *)error {
  89. _mapView.showsUserLocation = YES;//显示定位图层
  90. self.userLocation.location = location.location;
  91. [_mapView updateLocationData:_userLocation];
  92. _mapView.centerCoordinate = _userLocation.location.coordinate;
  93. [_locService stopUpdatingLocation];
  94. _coordinate = _userLocation.location.coordinate;
  95. self.annotation.coordinate = _coordinate;
  96. [_mapView addAnnotation:_annotation];
  97. //发起反向地理编码检索
  98. _searcher =[[BMKGeoCodeSearch alloc]init];
  99. _searcher.delegate = self;
  100. BMKReverseGeoCodeSearchOption *reverseGeoCodeSearchOption = [[BMKReverseGeoCodeSearchOption alloc]init];
  101. reverseGeoCodeSearchOption.location = _userLocation.location.coordinate;
  102. [_searcher reverseGeoCode:reverseGeoCodeSearchOption];
  103. }
  104. //返回反地理编码搜索结果
  105. - (void)onGetReverseGeoCodeResult:(BMKGeoCodeSearch *)searcher result:(BMKReverseGeoCodeSearchResult *)result errorCode:(BMKSearchErrorCode)error {
  106. [MBProgressHUD hideHUDForView:self.tableView animated:YES];
  107. searcher.delegate = nil;
  108. if (error == BMK_SEARCH_NO_ERROR)
  109. {
  110. // 商业圈 businessCircle
  111. // 街道号码 streetNumber
  112. // 街道名称 streetName
  113. // 区县名称 district
  114. // 城市名称 city
  115. // 省份名称 province;
  116. // 地址全称 address
  117. //地址周边POI信息,成员类型为BMKPoiInfo poiList
  118. /*
  119. NSString* _name; ///<POI名称
  120. NSString* _uid;
  121. NSString* _address; ///<POI地址
  122. NSString* _city; ///<POI所在城市
  123. NSString* _phone; ///<POI电话号码
  124. NSString* _postcode; ///<POI邮编
  125. int _epoitype; ///<POI类型,0:普通点 1:公交站 2:公交线路 3:地铁站 4:地铁线路
  126. CLLocationCoordinate2D _pt; ///<POI坐标
  127. */
  128. //NSLog(@"---->%@---->%@---->%@---->%@---->%@---->%@----->%@",result.addressDetail.streetNumber,result.addressDetail.streetName,result.addressDetail.district,result.addressDetail.city,result.addressDetail.province,result.address,result.poiList);
  129. //在此处理正常结果
  130. NSString *detailStr = [NSString stringWithFormat:@"%@%@%@",result.addressDetail.district,result.addressDetail.streetName,result.addressDetail.streetNumber];
  131. BMKPoiInfo *info = [[BMKPoiInfo alloc]init];
  132. info.name = result.address;
  133. info.address = detailStr;
  134. info.pt = result.location;
  135. //将周边存起来 微调的时候用
  136. _poiInfos = [NSMutableArray arrayWithArray:result.poiList];
  137. [_poiInfos insertObject:info atIndex:0];
  138. [self.tableView reloadData];
  139. [self.tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:NO scrollPosition:UITableViewScrollPositionNone];
  140. }
  141. }
  142. #pragma mark tableView delegate
  143. -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  144. {
  145. return _poiInfos.count;
  146. }
  147. -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
  148. return 61;
  149. }
  150. -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  151. {
  152. LocSeriviceCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
  153. if (cell == nil)
  154. {
  155. cell = [[[NSBundle mainBundle] loadNibNamed:@"LocSeriviceCell" owner:nil options:nil] lastObject];
  156. }
  157. BMKPoiInfo *poiInfo = _poiInfos[indexPath.row];
  158. cell.nameLab.text = poiInfo.name;
  159. cell.detailAddress.text = poiInfo.address;
  160. return cell;
  161. }
  162. -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  163. {
  164. BMKPoiInfo *info = _poiInfos[indexPath.row];
  165. // _mapView.centerCoordinate = info.pt;
  166. _coordinate = info.pt;
  167. [UIView animateWithDuration:1.0 animations:^{
  168. _annotation.coordinate = info.pt;
  169. }];
  170. }
  171. - (BMKUserLocation *)userLocation {
  172. if (!_userLocation) {
  173. _userLocation = [[BMKUserLocation alloc] init];
  174. }
  175. return _userLocation;
  176. }
  177. @end