// // LocServiceVC.m // LN_School // // Created by EchoShacolee on 2017/12/16. // Copyright © 2017年 Danson. All rights reserved. // #import "LocServiceVC.h" #import "LocSeriviceCell.h" #import //引入地图功能所有的头文件 #import //引入定位功能所有的头文件(新) #import //引入检索功能所有的头文件 @interface LocServiceVC () { CLLocationCoordinate2D _coordinate; BMKGeoCodeSearch *_searcher; BMKLocationManager *_locService; NSMutableArray *_poiInfos; } @property (nonatomic, strong) BMKUserLocation *userLocation;//当前位置对象 @property(nonatomic,strong)BMKPointAnnotation * annotation; @end @implementation LocServiceVC -(BMKPointAnnotation *)annotation{ if (!_annotation) { _annotation = [[BMKPointAnnotation alloc]init]; } return _annotation; } - (void)viewDidLoad { [super viewDidLoad]; self.navigationItem.title = @"位置"; [self goBackByNavigation]; [self customRightBtn]; self.tableView.tableFooterView = [UIView new]; _poiInfos = [NSMutableArray array]; //定位功能 _locService = [[BMKLocationManager alloc]init]; _locService.distanceFilter = 10;//将被通知任何移动(若=10,表示超出10米才会回调位置信息) _locService.delegate = self; [_locService startUpdatingLocation]; MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.tableView animated:YES]; // hud.label.text = @"正在检索..."; hud.backgroundColor = [UIColor whiteColor]; hud.removeFromSuperViewOnHide = YES; } -(void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; [_mapView viewWillAppear]; _mapView.delegate = self; // 此处记得不用的时候需要置nil,否则影响内存的释放 } -(void)viewWillDisappear:(BOOL)animated { [super viewWillDisappear:animated]; [_mapView viewWillDisappear]; _mapView.delegate = nil; // 不用时,置nil _searcher.delegate = nil; } -(void)customRightBtn{ UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem]; btn.frame = CGRectMake(0, 0, 43, 44); btn.titleLabel.font = [UIFont systemFontOfSize:17]; [btn setTitle:@"上传" forState:UIControlStateNormal]; [btn setTitleColor:defGreen forState:UIControlStateNormal]; [btn addTarget:self action:@selector(rightBtnClick) forControlEvents:UIControlEventTouchUpInside]; self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:btn]; } - (void)rightBtnClick { if (!_coordinate.latitude) { ShowMsg(@"未选择位置"); return; } if (self.block) { self.block(_coordinate.longitude, _coordinate.latitude); [self.navigationController popViewControllerAnimated:YES]; } } #pragma mark 定位相关 /**用户拒绝开启定位服务等原因导致的定位失败会调用的方法 */ - (void)BMKLocationManager:(BMKLocationManager *)manager didFailWithError:(NSError *)error { UIAlertView *alert =[[UIAlertView alloc]initWithTitle:@"温馨提示" message:@"未打开位置访问权限" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles: nil];//@"请在iPhone的“设置”-“隐私”-“定位服务”功能中,找到“优易学车”打开位置访问权限" [alert show]; [_locService startUpdatingLocation]; } //处理位置坐标更新 - (void)BMKLocationManager:(BMKLocationManager *)manager didUpdateLocation:(BMKLocation *)location orError:(NSError *)error { _mapView.showsUserLocation = YES;//显示定位图层 self.userLocation.location = location.location; [_mapView updateLocationData:_userLocation]; _mapView.centerCoordinate = _userLocation.location.coordinate; [_locService stopUpdatingLocation]; _coordinate = _userLocation.location.coordinate; self.annotation.coordinate = _coordinate; [_mapView addAnnotation:_annotation]; //发起反向地理编码检索 _searcher =[[BMKGeoCodeSearch alloc]init]; _searcher.delegate = self; BMKReverseGeoCodeSearchOption *reverseGeoCodeSearchOption = [[BMKReverseGeoCodeSearchOption alloc]init]; reverseGeoCodeSearchOption.location = _userLocation.location.coordinate; [_searcher reverseGeoCode:reverseGeoCodeSearchOption]; } //返回反地理编码搜索结果 - (void)onGetReverseGeoCodeResult:(BMKGeoCodeSearch *)searcher result:(BMKReverseGeoCodeSearchResult *)result errorCode:(BMKSearchErrorCode)error { [MBProgressHUD hideHUDForView:self.tableView animated:YES]; searcher.delegate = nil; if (error == BMK_SEARCH_NO_ERROR) { // 商业圈 businessCircle // 街道号码 streetNumber // 街道名称 streetName // 区县名称 district // 城市名称 city // 省份名称 province; // 地址全称 address //地址周边POI信息,成员类型为BMKPoiInfo poiList /* NSString* _name; ///%@---->%@---->%@---->%@---->%@---->%@----->%@",result.addressDetail.streetNumber,result.addressDetail.streetName,result.addressDetail.district,result.addressDetail.city,result.addressDetail.province,result.address,result.poiList); //在此处理正常结果 NSString *detailStr = [NSString stringWithFormat:@"%@%@%@",result.addressDetail.district,result.addressDetail.streetName,result.addressDetail.streetNumber]; BMKPoiInfo *info = [[BMKPoiInfo alloc]init]; info.name = result.address; info.address = detailStr; info.pt = result.location; //将周边存起来 微调的时候用 _poiInfos = [NSMutableArray arrayWithArray:result.poiList]; [_poiInfos insertObject:info atIndex:0]; [self.tableView reloadData]; [self.tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:NO scrollPosition:UITableViewScrollPositionNone]; } } #pragma mark tableView delegate -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return _poiInfos.count; } -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ return 61; } -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { LocSeriviceCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"]; if (cell == nil) { cell = [[[NSBundle mainBundle] loadNibNamed:@"LocSeriviceCell" owner:nil options:nil] lastObject]; } BMKPoiInfo *poiInfo = _poiInfos[indexPath.row]; cell.nameLab.text = poiInfo.name; cell.detailAddress.text = poiInfo.address; return cell; } -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { BMKPoiInfo *info = _poiInfos[indexPath.row]; // _mapView.centerCoordinate = info.pt; _coordinate = info.pt; [UIView animateWithDuration:1.0 animations:^{ _annotation.coordinate = info.pt; }]; } - (BMKUserLocation *)userLocation { if (!_userLocation) { _userLocation = [[BMKUserLocation alloc] init]; } return _userLocation; } @end