// // NeighbouringVC.m // jiaPei // // Created by apple on 2017/6/27. // Copyright © 2017年 JCZ. All rights reserved. // #import "NeighbouringVC.h" #import "NeighbourTableView.h" #import "NeighbourCell.h" #import "NeighbourBMKPointAnnotation.h" #import "NeighbourBMKAnnotationView.h" #import "SchDetailVC.h" #import "SearchBase.h" #import "ApplyVC.h" #import //引入地图功能所有的头文件 #import //引入定位功能所有的头文件(新) //#import #import "BMKClusterManager.h"//点聚合管理类 #define kAlphaNum @"0123456789."//限制输入字符类型 /* *点聚合Annotation */ @interface ClusterAnnotation : NeighbourBMKPointAnnotation ///所包含annotation个数 @property (nonatomic, assign) NSInteger size; @property (nonatomic, strong) NSArray *dataArr; @end @implementation ClusterAnnotation @synthesize size = _size; @end @interface NeighbouringVC () { BMKMapView *_mapView; CLLocation *myLocation; //搜索中心点 BMKPointAnnotation *_movePoint; BMKPinAnnotationView *_movePointView; //点聚合 BMKClusterManager *_clusterManager;//点聚合管理类 NSInteger _clusterZoom;//聚合级别 NSMutableArray *_clusterCaches;//点聚合缓存标注 NSArray *_tableVDataSource;//点聚合对应数据 ClusterAnnotation *_currentSelectedAnnotion; NeighbourTableView *mainTableView; UIButton *moreBtn; NSArray *dataArray; NSString *distanceString;//搜索范围 NSString *searchHistoryDistance;//历史搜索输入记录 CGFloat bothStateMapHeight; CGFloat mapDisY; NSInteger tableState; // 1全屏 2半屏 3消失 BOOL isLoading; UIPanGestureRecognizer *mapPan; UIPanGestureRecognizer *tableViewPan; CGFloat bottomBar_height;//底部限制高度 } @property (nonatomic, strong) NeighbourCell *detailCell; @property (nonatomic, strong) UIView *btnBar; @end @implementation NeighbouringVC - (void)dealloc{ [_movePointView removeObserver:self forKeyPath:@"dragState"]; } - (void)viewDidLoad { [super viewDidLoad]; self.navigationItem.title = @"附近报名点"; self.view.backgroundColor = backGroundColor; // [self configNavigationBar]; bottomBar_height = _isPushed ? kSafeAreaBottomHeight : kTabBarHeight; if (_isPushed) { [self configNavigationBar]; } [self myInit]; } -(void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; [_mapView viewWillAppear]; _mapView.delegate = self; // 此处记得不用的时候需要置nil,否则影响内存的释放 if (IOS11) { mainTableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever; }else{ self.automaticallyAdjustsScrollViewInsets = NO; } self.navigationController.navigationBar.translucent = YES; } -(void)viewWillDisappear:(BOOL)animated { self.navigationController.navigationBar.translucent = NO; [super viewWillDisappear:animated]; self.hidesBottomBarWhenPushed = NO; [_mapView viewWillDisappear]; _mapView.delegate = nil; // 不用时,置nil } - (void)myInit { UIBarButtonItem *item = [UIBarButtonItem rq_systemItemWithTitle:@"选择距离" titleColor:defGreen imageName:@"" target:self selector:@selector(changeDistance) textType:YES]; self.navigationItem.rightBarButtonItem = item; bothStateMapHeight = (kSize.height - kNavOffSet - bottomBar_height)*0.4; /* 全屏地图的中心点:kSize.height/2.0 - 50 - bottomBar_height + kNavOffSet; 半屏地图的中心点:kNavOffSet + bothStateMapHeight/2.0; 差值:kSize.height/2.0 - 50 - bottomBar_height + kNavOffSet - kNavOffSet - bothStateMapHeight/2.0 = kSize.height/2.0 - 50 - bottomBar_height - bothStateMapHeight/2.0 */ mapDisY = kSize.height/2.0 - 50 - bottomBar_height - bothStateMapHeight/2.0; //查看更多地图 UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, kSize.height - bottomBar_height - 50 - mapDisY, kSize.width, 50)]; [label setText:@"手指松开查看全屏地图" Font:FontLarger TextColor:kTitleColor Alignment:NSTextAlignmentCenter]; [self.view addSubview:label]; _mapView = [[BMKMapView alloc]initWithFrame:CGRectMake(0, kNavOffSet - mapDisY, kSize.width, kSize.height -kNavOffSet - bottomBar_height - 50)]; _mapView.mapType = BMKMapTypeStandard; _mapView.showMapScaleBar = YES; _mapView.userTrackingMode = BMKUserTrackingModeNone; _mapView.zoomLevel = 14.7; _mapView.showsUserLocation = YES; BMKLocationViewDisplayParam *displayParam = [[BMKLocationViewDisplayParam alloc]init]; displayParam.isRotateAngleValid = true;//跟随态旋转角度是否生效 displayParam.isAccuracyCircleShow = false;//精度圈是否显示 displayParam.locationViewOffsetX = 0;//定位偏移量(经度) displayParam.locationViewOffsetY = 0;//定位偏移量(纬度) [_mapView updateLocationViewWithParam:displayParam]; [self.view addSubview:_mapView]; //点聚合管理类 _clusterManager = [[BMKClusterManager alloc] init]; [self createClus];//点聚合缓存 mapPan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(mapGesTure)]; [_mapView addGestureRecognizer:mapPan]; isLoading = NO; tableState = 2; dataArray = [NSArray array]; distanceString = @"3000"; //定位功能 [self getMylocation]; UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom]; btn.frame = CGRectMake(kSize.width-70, _mapView.height-60, 37, 37); btn.backgroundColor = [UIColor whiteColor]; [btn setImage:[UIImage imageNamed:@"nei_meLocation.png"] forState:UIControlStateNormal]; btn.contentEdgeInsets = UIEdgeInsetsMake(7, 7, 7, 7); btn.layer.cornerRadius = 37/2; btn.layer.shadowOffset = CGSizeMake(2, 2); btn.layer.shadowRadius = 2; btn.layer.shadowColor = [UIColor darkGrayColor].CGColor;//shadowColor阴影颜色 btn.layer.shadowOpacity = 0.8;//阴影透明度,默认0 [btn addTarget:self action:@selector(getMylocation) forControlEvents:UIControlEventTouchUpInside]; [_mapView addSubview:btn]; //表 mainTableView = [[NeighbourTableView alloc] initWithFrame:CGRectMake(0, kNavOffSet + bothStateMapHeight, kSize.width, kSize.height - kNavOffSet - bottomBar_height) style:UITableViewStylePlain]; mainTableView.delegate = self; mainTableView.dataSource = self; mainTableView.isCanPan = NO; mainTableView.rowHeight = 125; mainTableView.tableFooterView = [UIView new]; [self.view addSubview:mainTableView]; tableViewPan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(tableViewGesTure:)]; tableViewPan.delegate = self; [self.view addGestureRecognizer:tableViewPan]; } - (void)changeDistance { UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:@"请选择搜索距离" preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction *search1Action = [UIAlertAction actionWithTitle:@"3km" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { distanceString = @"3000"; [self getSchPoints]; }]; UIAlertAction *search2Action = [UIAlertAction actionWithTitle:@"4km" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { distanceString = @"4000"; [self getSchPoints]; }]; UIAlertAction *search3Action = [UIAlertAction actionWithTitle:@"5km" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { distanceString = @"5000"; [self getSchPoints]; }]; UIAlertAction *search4Action = [UIAlertAction actionWithTitle:@"10km" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { distanceString = @"10000"; [self getSchPoints]; }]; UIAlertAction *search5Action = [UIAlertAction actionWithTitle:@"自定义 >" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) { [self setSearchDistance]; }]; UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]; [alert addAction:search1Action]; [alert addAction:search2Action]; [alert addAction:search3Action]; [alert addAction:search4Action]; [alert addAction:search5Action]; [alert addAction:cancelAction]; [self presentViewController:alert animated:YES completion:nil]; } -(void)setSearchDistance{ UIAlertController *alertFind = [UIAlertController alertControllerWithTitle:@"自定义搜索范围" message:@"例如:5.2km以内,则输入“5.2”" preferredStyle:UIAlertControllerStyleAlert]; [alertFind addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) { textField.placeholder = @"请输入搜素范围公里数___(km)"; if (searchHistoryDistance) { textField.placeholder = searchHistoryDistance; } textField.delegate = self; textField.keyboardType = UIKeyboardTypeNumbersAndPunctuation; }]; [alertFind addAction:[UIAlertAction actionWithTitle:@"开始搜索" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { UITextField *tf = [alertFind textFields][0]; if ([tf.text floatValue] == 0) { ShowMsg(@"未输入搜索范围"); return; } searchHistoryDistance = tf.text; NSString *new = [NSString stringWithFormat:@"%lf",[tf.text floatValue]*1000]; if ([new isEqualToString:distanceString]) { return; } distanceString = new; [self getSchPoints]; }]]; [alertFind addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) { }]]; [self presentViewController:alertFind animated:true completion:nil]; } - (void)mapGesTure { //地图放至最大 表消失 [self setUITableViewDis]; } - (void)tableViewGesTure:(UIPanGestureRecognizer *)gesture { //获取滑动的距离 作出表格移动效果 如果向上移动 在最后 做mapDis操作 否则做tabdis操作 CGPoint point = [gesture translationInView:self.view];//相对父视图的所有偏移量 //NSLog(@"point-->(%f,%f)",point.x,point.y); if (bothStateMapHeight + point.y <= 0.0 || bothStateMapHeight + point.y > kSize.height - kNavOffSet - bottomBar_height - 50) { //滑到最上或者最下面了 直接调接口吧 if (point.y > 0) { [self setUITableViewDis]; }else { [self setUIMapDis]; } return; } if (tableState == 1) { if (mainTableView.y < kNavOffSet + bothStateMapHeight) { mainTableView.y = kNavOffSet + 2.4*point.y; }else { mainTableView.y = kNavOffSet + point.y; } }else if (tableState == 2) { mainTableView.y = kNavOffSet + bothStateMapHeight + point.y; }else { mainTableView.y = CGRectGetMaxY(_mapView.frame); } if (gesture.state == UIGestureRecognizerStateEnded || gesture.state == UIGestureRecognizerStateCancelled) { //滑动结束 判断滑动后最终状态 if (point.y < -10) { [self setUIMapDis]; }else if (point.y < 10){ [self setUIBothApper]; }else { [self setUITableViewDis]; } } } - (void)setUIMapDis { [_mapView removeGestureRecognizer:mapPan]; [self.view removeGestureRecognizer:tableViewPan]; mainTableView.y = kNavOffSet; mainTableView.isCanPan = YES; tableState = 1; } - (void)setUITableViewDis { if (_currentSelectedAnnotion && _currentSelectedAnnotion.size > 1) { [_mapView deselectAnnotation:_currentSelectedAnnotion animated:YES]; } [_mapView removeGestureRecognizer:mapPan]; [self.view removeGestureRecognizer:tableViewPan]; _mapView.y = kNavOffSet; mainTableView.y = CGRectGetMaxY(_mapView.frame); [self addLoadingMore]; tableState = 3; } - (void)addLoadingMore { if (![self.view.subviews containsObject:moreBtn]) { moreBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, kSize.height - bottomBar_height - 50, kSize.width, 50)]; moreBtn.backgroundColor = [UIColor whiteColor]; [moreBtn setTitle:@"点击展开更多结果" textColor:kTitleColor font:Font17 fotState:UIControlStateNormal]; [moreBtn target:self tag:1]; [self.view addSubview:moreBtn]; } } - (void)setUIBothApper { //移除 if (moreBtn.superview) { [moreBtn removeFromSuperview]; } //移除 if ([self.view.subviews containsObject:self.detailCell]) { [self.detailCell removeFromSuperview]; } //移除 if ([self.view.subviews containsObject:self.btnBar]) { [self.btnBar removeFromSuperview]; } if (mapPan) { [_mapView addGestureRecognizer:mapPan]; }else { mapPan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(mapGesTure)]; [_mapView addGestureRecognizer:mapPan]; } if (tableViewPan) { [self.view addGestureRecognizer:tableViewPan]; }else { tableViewPan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(tableViewGesTure:)]; [self.view addGestureRecognizer:tableViewPan]; } _mapView.y = kNavOffSet - mapDisY; mainTableView.y = kNavOffSet + bothStateMapHeight; mainTableView.isCanPan = NO; tableState = 2; } - (NeighbourCell *)detailCell { if (!_detailCell) { _detailCell = [[NeighbourCell alloc] initWithFrame:CGRectMake(0, kSize.height - bottomBar_height - 40 - 125 , kSize.width, 125)]; _detailCell.contentView.backgroundColor = [UIColor whiteColor]; UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0 , kSize.width, 125)]; [btn target:self tag:2]; [_detailCell.contentView addSubview:btn]; } return _detailCell; } - (UIView *)btnBar { if (!_btnBar) { _btnBar = [[UIView alloc] initWithFrame:CGRectMake(0, kSize.height - bottomBar_height - 40, kSize.width, 40)]; _btnBar.backgroundColor = [UIColor whiteColor]; NSArray *titleArray = @[@" 电话",@" 搜驾校",@" 报名"]; NSArray *imgArray = @[@"neighbour_tel",@"neighbour_search",@"neighbour_apply"]; [_btnBar addSelfViewWithRect:CGRectMake(0, 0, _btnBar.width, 1) Color:KlineColor]; for (int i = 0; i < titleArray.count; i ++) { UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(kSize.width/3.0*i, 0, kSize.width/3.0, 40)]; [btn setImage:[UIImage imageNamed:imgArray[i]] withTitle:titleArray[i] Font:FontTitle forState:UIControlStateNormal]; [btn target:self tag:3 + i]; [_btnBar addSubview:btn]; } } return _btnBar; } - (void)btnClick:(UIButton *)sender { if (sender.tag == 1) { if (dataArray.count == 0) { ShowMsg(@"指定范围没有报名点哦!"); return; } _tableVDataSource = dataArray; [self setUIBothApper]; } if (sender.tag == 2) { //跳转至详情页 SchDetailVC *vc = [[SchDetailVC alloc] init]; vc.schoolId = _detailCell.dataDic[@"SCHID"]; [self navPushHideTabbarToVC:vc]; } //三四五 分别是 电话 找驾校 报名 if (sender.tag == 3) { //电话 NSString *telephone = _detailCell.dataDic[@"PHONE"]; if (!telephone || telephone.length == 0) { ShowMsg(@"暂未获取到联系电话"); return; } UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:telephone preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction *telAction = [UIAlertAction actionWithTitle:@"呼叫" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { NSMutableString * url=[[NSMutableString alloc] initWithFormat:@"tel:%@",telephone]; [[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]]; }]; UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]; [alert addAction:telAction]; [alert addAction:cancelAction]; [self presentViewController:alert animated:YES completion:nil]; } if (sender.tag == 4) { SearchBase *vc = [[SearchBase alloc] init]; vc.type = 2; [self navPushHideTabbarToVC:vc]; } if (sender.tag == 5) { ApplyVC *vc = [[ApplyVC alloc] init]; vc.jxbh = _detailCell.dataDic[@"TSP_SCHOOL_ID"]; vc.jxmc = _detailCell.dataDic[@"SCHOOLNAME"]; [self navPushHideTabbarToVC:vc]; } } #pragma mark textfiledDelegate //输入限制 - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string; { //先设置只能输入的集合 invertedSet就是将咱们允许输入的字符串的字符找出 NSCharacterSet *canInputSet = [[NSCharacterSet characterSetWithCharactersInString:kAlphaNum] invertedSet]; //把允许输入的内容转化成数组,再转化成字符串 NSString *str = [[string componentsSeparatedByCharactersInSet:canInputSet] componentsJoinedByString:@""]; //判断输入的字符是否包含在允许输入的字符之内 BOOL isSuccess = [string isEqualToString:str]; //限制文本框输入内容的长度不得超过10且有输入内容的限制 if (textField.text.length <= 5 && isSuccess){ //返回值为YES的时候,文本框可以进行编辑 return YES; }else{ if (!isSuccess) { return NO; } if (str.length == 0) { return YES; } //str=nil 删除 return NO; } } #pragma mark - 定位 - (void)getMylocation { [[MapManager sharedManager] updateLocationWithCompleteBlock:^(BOOL success, CLLocation * _Nonnull location) { if (success) { [self->_mapView setCenterCoordinate:location.coordinate]; self->myLocation = location; if (self->dataArray.count == 0) { [self addMovePointForSearchWith:self->myLocation.coordinate]; //获取报名点信息 [self getSchPoints]; } } }]; } #pragma mark -添加大头针 -(void)addMovePointForSearchWith:(CLLocationCoordinate2D)coordinate{ if (_movePoint) { if (_movePoint.coordinate.latitude == coordinate.latitude && _movePoint.coordinate.longitude == coordinate.longitude) { return; } _movePoint.coordinate = coordinate; return; } BMKPointAnnotation *point = [[BMKPointAnnotation alloc]init]; _movePoint = point; point.title = @"在此点附近搜索"; point.subtitle = @"(长按移动位置)"; point.coordinate = coordinate; [_mapView addAnnotation:point]; } #pragma mark 地图相关 -(void)createClus{ _clusterCaches = [[NSMutableArray alloc] init]; for (NSInteger i = 3; i < 22; i++) { [_clusterCaches addObject:[NSMutableArray array]]; } } //加标注点 - (void)addAnnotation { //添加前先清理地图上之前的标注 if (_mapView.annotations.count > 0) { [_clusterManager clearClusterItems]; [self createClus]; } [_mapView setCenterCoordinate:_movePoint.coordinate]; for (NSDictionary *dic in dataArray) { BMKClusterItem *clusterItem = [[BMKClusterItem alloc] init]; CLLocationCoordinate2D coor; coor = CLLocationCoordinate2DMake([dic[@"TSP_LAT"] floatValue], [dic[@"TSP_LNG"] floatValue]); if (coor.latitude>53 || coor.latitude<4 || coor.longitude<73 || coor.longitude>135) { continue; } clusterItem.coor = coor; clusterItem.dataDic = dic; [_clusterManager addClusterItem:clusterItem]; } if (_movePoint) {//这个判断。。其实前面是定位成功才会走到这里面的 // 添加圆形覆盖物 [_mapView removeOverlays:_mapView.overlays]; double radius = [distanceString doubleValue]; BMKCircle *circle = [BMKCircle circleWithCenterCoordinate:_movePoint.coordinate radius:radius]; [_mapView addOverlay:circle]; //限制地图显示范围 以下方法只是计算个大概 测试还ok。所以就用了 /*关于经纬度十进制表示法 对于两个点,在纬度相等的情况下: 经度每隔0.00001度,距离相差约1米;每隔0.0001度,距离相差约10米;每隔0.001度,距离相差约100米;每隔0.01度,距离相差约1000米;每隔0.1度,距离相差约10000米。 对于两个点,在经度相等的情况下: 纬度每隔0.00001度,距离相差约1.1米;每隔0.0001度,距离相差约11米;每隔0.001度,距离相差约111米;每隔0.01度,距离相差约1113米;每隔0.1度,距离相差约11132米。*/ _mapView.maxZoomLevel = 21; _mapView.minZoomLevel = 3; CLLocationCoordinate2D center = _movePoint.coordinate; radius *= 1.3; CLLocationCoordinate2D left = CLLocationCoordinate2DMake(center.latitude-radius*0.00001, center.longitude); CLLocationCoordinate2D right = CLLocationCoordinate2DMake(center.latitude+radius*0.00001, center.longitude); BMKPointAnnotation *leftAn = [[BMKPointAnnotation alloc]init]; leftAn.coordinate = left; BMKPointAnnotation *rightAN = [[BMKPointAnnotation alloc]init]; rightAN.coordinate = right; [_mapView showAnnotations:@[leftAn,rightAN] animated:YES]; } [self updateClusters]; } //更新聚合状态 - (void)updateClusters { _clusterZoom = (NSInteger)_mapView.zoomLevel; @synchronized(_clusterCaches) { __block NSMutableArray *clusters = [_clusterCaches objectAtIndex:_clusterZoom - 3]; if (clusters.count > 0) { [self removeAnnotations]; [_mapView addAnnotations:clusters]; } else { dispatch_async(dispatch_get_global_queue(0, 0), ^{ ///获取聚合后的标注 __block NSArray *array = [_clusterManager getClusters:_clusterZoom]; if (array.count == 0) { return; } dispatch_async(dispatch_get_main_queue(), ^{ for (BMKCluster *item in array) { ClusterAnnotation *annotation = [[ClusterAnnotation alloc] init]; annotation.coordinate = item.coordinate; annotation.size = item.size; annotation.dataDic = item.dataDic; annotation.dataArr = item.dataArr; [clusters addObject:annotation]; } [self removeAnnotations]; [_mapView addAnnotations:clusters]; [_clusterCaches replaceObjectAtIndex:_clusterZoom - 3 withObject:clusters]; }); }); } } } //根据overlay生成对应的View - (BMKOverlayView *)mapView:(BMKMapView *)mapView viewForOverlay:(id )overlay { if ([overlay isKindOfClass:[BMKCircle class]]) { BMKCircleView* circleView = [[BMKCircleView alloc] initWithOverlay:overlay]; circleView.fillColor = [UIColor colorWithRed:35/255.0 green:160/255.0 blue:227/255.0 alpha:0.1]; // circleView.strokeColor = [UIColor lightGrayColor]; // circleView.lineWidth = 0.5; return circleView; } return nil; } //标注的代理 -(BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id)annotation { //普通annotation if (annotation == _movePoint) { NSString *AnnotationViewID = @"renameMark"; BMKPinAnnotationView *annotationView = (BMKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:AnnotationViewID]; if (annotationView == nil) { annotationView = [[BMKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationViewID]; _movePointView = annotationView; // 设置颜色 annotationView.pinColor = BMKPinAnnotationColorPurple; // 设置可拖拽 annotationView.draggable = YES; [annotationView addObserver:self forKeyPath:@"dragState" options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld context:nil]; } return annotationView; } if ([annotation isKindOfClass:[NeighbourBMKPointAnnotation class]]) { NeighbourBMKAnnotationView *view = [NeighbourBMKAnnotationView annotationViewWithMapView:mapView Annotation:(BMKPointAnnotation *)annotation]; view.block = ^(NeighbourBMKPointAnnotation *clickedAnnotation) { NSDictionary *dic = clickedAnnotation.dataDic; BMKOpenDrivingRouteOption *opt = [[BMKOpenDrivingRouteOption alloc] init]; opt.appScheme = @"baidumapsdk://mapsdk.baidu.com"; opt.isSupportWeb = YES;//调起百度地图客户端失败后,是否支持调起web地图,默认:YES //指定起点 BMKPlanNode *start = [[BMKPlanNode alloc]init]; start.pt = myLocation.coordinate; opt.startPoint = start; //指定终点名称 BMKPlanNode *end = [[BMKPlanNode alloc]init]; end.pt = CLLocationCoordinate2DMake([dic[@"TSP_LAT"] floatValue], [dic[@"TSP_LNG"] floatValue]); opt.endPoint = end; BMKOpenErrorCode code = [BMKOpenRoute openBaiduMapDrivingRoute:opt]; if (code == 1 || code == 0) { }else{ ShowMsg(@"打开百度地图失败"); } }; ClusterAnnotation *cluster = (ClusterAnnotation*)annotation; view.annotation = cluster;//这一步如果不写的话 拿到的view。annotation == nil;按道理是应该在前面获取view的地方已经传入了annotation view.size = cluster.size;//size依赖annotati的值 return view; } return nil; } /** * 当选中一个annotation views时,调用此接口 * @param mapView 地图View * @param views 选中的annotation views */ - (void)mapView:(BMKMapView *)mapView didSelectAnnotationView:(BMKAnnotationView *)view { //NeighbourBMKAnnotationView 点击处理 if ([view isKindOfClass:[NeighbourBMKAnnotationView class]]) { _currentSelectedAnnotion = (ClusterAnnotation *)view.annotation; [self setUITableViewDis]; ClusterAnnotation *annotation = (ClusterAnnotation *)view.annotation; if (annotation.dataArr.count > 1) { _tableVDataSource = annotation.dataArr; [mainTableView reloadData]; [self setUIBothApper]; return; }else{ NeighbourBMKPointAnnotation *annotation = (NeighbourBMKPointAnnotation *)view.annotation; if (![self.view.subviews containsObject:self.detailCell]) { //不知道为什么初始化时候的设置没起到作用 self.detailCell.frame = CGRectMake(0, kSize.height - bottomBar_height - 40 - 125 , kSize.width, 125); [self.view addSubview:self.detailCell]; } self.detailCell.dataDic = annotation.dataDic; if (![self.view.subviews containsObject:self.btnBar]) { [self.view addSubview:self.btnBar]; } } } } /** *地图初始化完毕时会调用此接口 *@param mapView 地图View */ - (void)mapViewDidFinishLoading:(BMKMapView *)mapView { [self updateClusters]; } /** * 选中气泡调用方法 * @param mapView 地图 * @param view annotation */ - (void)mapView:(BMKMapView *)mapView annotationViewForBubble:(BMKAnnotationView *)view { if (view == _movePointView) { [_mapView deselectAnnotation:view.annotation animated:YES]; [self changeDistance]; return; } if ([view isKindOfClass:[NeighbourBMKAnnotationView class]]) { ClusterAnnotation *annotation = (ClusterAnnotation *)view.annotation; NSDictionary *dic = annotation.dataDic; SchDetailVC *vc = [[SchDetailVC alloc] init]; vc.schoolId = dic[@"SCHID"]; [self navPushHideTabbarToVC:vc]; } } /** *地图渲染每一帧画面过程中,以及每次需要重绘地图时(例如添加覆盖物)都会调用此接口 *@param mapView 地图View *@param status 此时地图的状态 */ - (void)mapView:(BMKMapView *)mapView onDrawMapFrame:(BMKMapStatus *)status { if (_clusterZoom != 0 && _clusterZoom != (NSInteger)mapView.zoomLevel) { [self updateClusters]; } } - (void)mapView:(BMKMapView *)mapView onClickedMapBlank:(CLLocationCoordinate2D)coordinate{ //移除 if ([self.view.subviews containsObject:self.detailCell]) { [self.detailCell removeFromSuperview]; } if ([self.view.subviews containsObject:self.btnBar]) { [self.btnBar removeFromSuperview]; } [self setUITableViewDis]; } //移除anantions - (void)removeAnnotations{ NSMutableArray *annotations = [NSMutableArray arrayWithArray:_mapView.annotations]; if ([annotations containsObject:_movePoint]) { [annotations removeObject:_movePoint]; } [_mapView removeAnnotations:annotations]; } #pragma mark - 监听movePoint的拖动状态 - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context{ if([keyPath isEqualToString:@"dragState"]) { // NSLog(@"赋值后--%lu",(unsigned long)_movePointView.dragState); // NSLog(@"新的值--%@",change[@"new"]); // NSLog(@"以前的值--%@",change[@"old"]); NSString *new = [NSString stringWithFormat:@"%@",change[@"new"]]; NSString *old = [NSString stringWithFormat:@"%@",change[@"old"]]; if ([new integerValue] == BMKAnnotationViewDragStateEnding && [old integerValue] == BMKAnnotationViewDragStateDragging) { [_mapView selectAnnotation:_movePoint animated:YES]; } } } #pragma mark tableview -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return _tableVDataSource.count; } -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { NeighbourCell *cell = [tableView dequeueReusableCellWithIdentifier:@"neighbourCell"]; if (cell == nil) { cell = [[NeighbourCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"neighbourCell"]; } cell.dataDic = _tableVDataSource[indexPath.row]; return cell; } -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { NSDictionary *dic = _tableVDataSource[indexPath.row]; SchDetailVC *vc = [[SchDetailVC alloc] init]; vc.schoolId = dic[@"SCHID"]; [self navPushHideTabbarToVC:vc]; } #pragma mark 数据请求 -(void)getSchPoints { [self setUITableViewDis]; if ([distanceString floatValue] == 0) { return; } if (![Util connectedToNetWork]) { showMsgUnconnect(); return; } if (isLoading) { return; } isLoading = YES; NSMutableArray *arr=[NSMutableArray array]; [arr addObject:[NSDictionary dictionaryWithObjectsAndKeys:[NSString stringWithFormat:@"%f",_movePoint.coordinate.latitude] ,@"lat", nil]]; [arr addObject:[NSDictionary dictionaryWithObjectsAndKeys:[NSString stringWithFormat:@"%f",_movePoint.coordinate.longitude] ,@"lng", nil]]; [arr addObject:[NSDictionary dictionaryWithObjectsAndKeys:distanceString,@"mile", nil]]; NSString* method = @"getSchPoints"; [MBProgressHUD showMessage:@"正在搜索报名点..." toView:self.view]; self.navigationItem.rightBarButtonItem.enabled = NO; [jiaPeiManager requestAnythingWithURL:method array:arr data:nil completion:^(NSDictionary * root) { self.navigationItem.rightBarButtonItem.enabled = YES; [MBProgressHUD hideHUDForView:self.view]; isLoading = NO; if (!root) { ShowMsgFailed(); return; } if ( 1 == [root[@"code"] integerValue]) { ShowMsg(root[@"body"]); return; } if ([root[@"body"] isKindOfClass:[NSString class]]) { NSString *string = root[@"body"]; NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding]; NSError *error; dataArray = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error]; }else { dataArray = [NSArray arrayWithArray:root[@"body"]]; } //NSLog(@"附近的报名点-----><>%@",dataArray); if ([dataArray isKindOfClass:[NSArray class]]) { if (dataArray.count!=0) { [self setUIBothApper]; }else{ ShowMsg(@"附近暂无报名点"); [self setUITableViewDis]; } _tableVDataSource = dataArray; [mainTableView reloadData]; [self addAnnotation]; } /* {"PRICE":"2980", 价格 "TSP_MEMO":"价格5500", 备注 "STAR":"4", 报名点星级 "TSP_LOGOUT":"0", "DIS":"3198.5", 距离 "SCHOOLNAME":"沈阳市八棵树汽车驾驶员培训有限公司", "WATCH":"799", 关注度 "PHONE":"15960109842", 电话 "TSP_LNG":"119.268745", 经度 "SHORTNAME":"沈阳八棵树驾校", 驾校简称 "TSP_NAME":"软件园报名点", 报名点名称 "TSP_ADDRESS":"福州市鼓楼区软件园B区12号楼", 地址 "TSP_ID":"10042", "TSP_LAT":"26.115313", 纬度 "TSP_SCHOOL_ID":"2101000003"}。 驾校ID */ //将附近点加入标注 并且刷新表 }]; } - (void)scrollViewDidScroll:(UIScrollView *)scrollView { // NSLog(@"scroll-->>%f-%f",scrollView.contentOffset.x,scrollView.contentOffset.y); if (scrollView.contentOffset.y < -70) { [UIView animateWithDuration:.3 animations:^{ mainTableView.y = kNavOffSet + bothStateMapHeight; } completion:^(BOOL finished) { [self setUIBothApper]; }]; } } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; NSLog(@"didReceiveMemoryWarning BMKMap"); } @end