close
当前位置: 物联网在线 > 技术文库 > ios >

iOS-百度地图自定义气泡

iOS-百度地图自定义气泡

Paste_Image.png

文章简单实现百度地图添加标注和自定义气泡的功能,基本都是SDK 提供的方法,不做详细解释。

很久没有更新,具体教程请以官网文档为准,本文只提供设计思路。

1.添加多个标注和自定义气泡

添加自定义标注

[_mapView addAnnotations:array];

实现代理方法

#pragma mark -- BMKMapdelegate /** *根据anntation生成对应的View *@param mapView 地图View *@param annotation 指定的标注 *@return 生成的标注View */ -(BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id <BMKAnnotation>)annotation { if ([annotation isKindOfClass:[BMKPointAnnotation class]]) { BMKPinAnnotationView *newAnnotationView = [[BMKPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:@"myAnnotation"]; newAnnotationView.animatesDrop = YES; newAnnotationView.annotation = annotation; //这里我根据自己需要,继承了BMKPointAnnotation,添加了标注的类型等需要的信息 MyBMKPointAnnotation *tt = (MyBMKPointAnnotation *)annotation; //判断类别,需要添加不同类别,来赋予不同的标注图片 if (tt.profNumber == 100000) { newAnnotationView.image = [UIImage imageNamed:@"ic_map_mode_category_merchants_normal.png"]; }else if (tt.profNumber == 100001){ } //设定popView的高度,根据是否含有缩略图 double popViewH = 60; if (annotation.subtitle == nil) { popViewH = 38; } UIView *popView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth-100, popViewH)]; popView.backgroundColor = [UIColor whiteColor]; [popView.layer setMasksToBounds:YES]; [popView.layer setCornerRadius:3.0]; popView.alpha = 0.9; //自定义气泡的内容,添加子控件在popView上 UILabel *driverName = [[UILabel alloc]initWithFrame:CGRectMake(8, 4, 160, 30)]; driverName.text = annotation.title; driverName.numberOfLines = 0; driverName.backgroundColor = [UIColor clearColor]; driverName.font = [UIFont systemFontOfSize:15]; driverName.textColor = [UIColor blackColor]; driverName.textAlignment = NSTextAlignmentLeft; [popView addSubview:driverName]; UILabel *carName = [[UILabel alloc]initWithFrame:CGRectMake(8, 30, 180, 30)]; carName.text = annotation.subtitle; carName.backgroundColor = [UIColor clearColor]; carName.font = [UIFont systemFontOfSize:11]; carName.textColor = [UIColor lightGrayColor]; carName.textAlignment = NSTextAlignmentLeft; [popView addSubview:carName]; if (annotation.subtitle != nil) { UIButton *searchBn = [[UIButton alloc]initWithFrame:CGRectMake(170, 0, 50, 60)]; [searchBn setTitle:@"查看路线" forState:UIControlStateNormal]; searchBn.backgroundColor = mainColor; searchBn.titleLabel.numberOfLines = 0; [searchBn addTarget:self action:@selector(searchLine)]; [popView addSubview:searchBn]; } BMKActionPaopaoView *pView = [[BMKActionPaopaoView alloc]initWithCustomView:popView]; pView.frame = CGRectMake(0, 0, ScreenWidth-100, popViewH); ((BMKPinAnnotationView*)newAnnotationView).paopaoView = nil; ((BMKPinAnnotationView*)newAnnotationView).paopaoView = pView; return newAnnotationView; } return nil; }

实现点击标注和气泡协议

/** * 当选中一个annotation views时,调用此接口 * @param mapView 地图View * @param views 选中的annotation views */ - (void)mapView:(BMKMapView *)mapView didSelectAnnotationView:(BMKAnnotationView *)view { _shopCoor = view.annotation.coordinate; } /** * 选中气泡调用方法 * @param mapView 地图 * @param view annotation */ - (void)mapView:(BMKMapView *)mapView annotationViewForBubble:(BMKAnnotationView *)view { MyBMKPointAnnotation *tt = (MyBMKPointAnnotation *)view.annotation; if (tt.shopID) { BusinessIfonUVC *BusinessIfonVC = [[BusinessIfonUVC alloc]init]; BusinessIfonVC.shopId = tt.shopID; [self.navigationController pushViewController:BusinessIfonVC animated:YES]; } }

2.实现路线搜索,路径规划,获取街道名称等功能

通过经纬度获取地址,逆地理编码

-(void)getStartAddress { //起点地址 CLGeocoder *Geocoder = [[CLGeocoder alloc]init]; CLGeocodeCompletionHandler handler = ^(NSArray *place,NSError *error){ for(CLPlacemark *placemark in place){ NSString *tmp = [[NSString alloc]init]; tmp = placemark.subThoroughfare; if (tmp == nil) { tmp = @""; } NSString *startAdr = [[NSString alloc]initWithFormat:@"%@%@",placemark.thoroughfare,tmp]; _startCityText.text = placemark.locality; if ([startAdr isEqualToString:@"(null)"]) { _startAddrText.text = @"获取地址失败"; }else{ _startAddrText.text = startAdr; } } }; CLLocation *loc = [[CLLocation alloc]initWithLatitude:self.startCoor.latitude longitude:self.startCoor.longitude]; [Geocoder reverseGeocodeLocation:loc completionHandler:handler]; }

路径检索

- (void)onGetTransitRouteResult:(BMKRouteSearch*)searcher result:(BMKTransitRouteResult*)result errorCode:(BMKSearchErrorCode)error { NSMutableArray *lineArr = [[NSMutableArray alloc]init]; NSArray* array = [NSArray arrayWithArray:_mapView.annotations]; [_mapView removeAnnotations:array]; array = [NSArray arrayWithArray:_mapView.overlays]; [_mapView removeOverlays:array]; if (error == BMK_SEARCH_NO_ERROR) { for(int j = 0; j < [result.routes count];j++) { NSMutableArray *busTitleArr = [[NSMutableArray alloc]init]; NSMutableArray *lineStepsArr = [[NSMutableArray alloc]init]; NSMutableArray *stepsArr = [[NSMutableArray alloc]init]; //生成数据模型 LineInfoModel *lineInfo = [[LineInfoModel alloc]init]; BMKTransitRouteLine* plan = (BMKTransitRouteLine*)[result.routes objectAtIndex:j]; //数据模型:获得路线长度 lineInfo.distance = plan.distance; //数据模型:获得路线消耗时间 lineInfo.dates = plan.duration.dates; lineInfo.hours = plan.duration.hours; lineInfo.minutes = plan.duration.minutes; lineInfo.seconds = plan.duration.seconds; // 获得轨迹点 lineInfo.planStepsArr = plan.steps; // 计算路线方案中的路段数目 int size = [plan.steps count]; int planPointCounts = 0; for (int i = 0; i < size; i++) { BMKTransitStep* transitStep = [plan.steps objectAtIndex:i]; //数据模型:获得乘坐公交数组 DLog(@"%@",transitStep.vehicleInfo.title); if (transitStep.vehicleInfo.title) { [busTitleArr addObject:transitStep.vehicleInfo.title]; } //数据模型:获取换乘信息 if (transitStep.instruction) { transitStep.instruction = [transitStep.instruction stringByReplacingOccurrencesOfString:@"<font color=\"#313233\">" withString:@""]; [lineStepsArr addObject:transitStep.instruction]; } DLog(@"%@ %@",transitStep.vehicleInfo.title,transitStep.instruction); if(i==0){ RouteAnnotation* item = [[RouteAnnotation alloc]init]; item.coordinate = plan.starting.location; item.title = @"起点"; item.type = 0; // [_mapView addAnnotation:item]; // 添加起点标注 // // [stepsArr addObject:item]; }else if(i==size-1){ RouteAnnotation* item = [[RouteAnnotation alloc]init]; item.coordinate = plan.terminal.location; item.title = @"终点"; item.type = 1; [stepsArr addObject:item]; // [_mapView addAnnotation:item]; // 添加起点标注 // } RouteAnnotation* item = [[RouteAnnotation alloc]init]; item.coordinate = transitStep.entrace.location; item.title = transitStep.instruction; transitStep.instruction = [transitStep.instruction stringByReplacingOccurrencesOfString:@"<font color=\"#313233\">" withString:@""]; item.type = 3; [stepsArr addObject:item]; // [_mapView addAnnotation:item]; // // // //轨迹点总数累计 planPointCounts += transitStep.pointsCount; } lineInfo.vehicleInfoArr = busTitleArr; lineInfo.lineStepsArr = lineStepsArr; lineInfo.stepsArr = stepsArr; lineInfo.planPointCounts = planPointCounts; [lineArr addObject:lineInfo]; // //轨迹点 // BMKMapPoint * temppoints = new BMKMapPoint[planPointCounts]; // int i = 0; // for (int j = 0; j < size; j++) { // BMKTransitStep* transitStep = [plan.steps objectAtIndex:j]; // int k=0; // for(k=0;k<transitStep.pointsCount;k++) { // temppoints[i].x = transitStep.points[k].x; // temppoints[i].y = transitStep.points[k].y; // i++; // } } self.lineStatusArr = lineArr; // 通过points构建BMKPolyline // BMKPolyline* polyLine = [BMKPolyline polylineWithPoints:temppoints count:planPointCounts]; // [_mapView addOverlay:polyLine]; // 添加路线overlay // delete []temppoints; // } [_tableView reloadData]; } } - (void)onGetDrivingRouteResult:(BMKRouteSearch*)searcher result:(BMKDrivingRouteResult*)result errorCode:(BMKSearchErrorCode)error { NSArray* array = [NSArray arrayWithArray:_mapView.annotations]; [_mapView removeAnnotations:array]; array = [NSArray arrayWithArray:_mapView.overlays]; [_mapView removeOverlays:array]; if (error == BMK_SEARCH_NO_ERROR) { BMKDrivingRouteLine* plan = (BMKDrivingRouteLine*)[result.routes objectAtIndex:0]; // 计算路线方案中的路段数目 int size = [plan.steps count]; int planPointCounts = 0; for (int i = 0; i < size; i++) { BMKDrivingStep* transitStep = [plan.steps objectAtIndex:i]; if(i==0){ RouteAnnotation* item = [[RouteAnnotation alloc]init]; item.coordinate = plan.starting.location; item.title = @"起点"; item.type = 0; [_mapView addAnnotation:item]; // 添加起点标注 }else if(i==size-1){ RouteAnnotation* item = [[RouteAnnotation alloc]init]; item.coordinate = plan.terminal.location; item.title = @"终点"; item.type = 1; [_mapView addAnnotation:item]; // 添加起点标注 } //添加annotation节点 RouteAnnotation* item = [[RouteAnnotation alloc]init]; item.coordinate = transitStep.entrace.location; item.title = transitStep.entraceInstruction; item.degree = transitStep.direction * 30; item.type = 4; [_mapView addAnnotation:item]; //轨迹点总数累计 planPointCounts += transitStep.pointsCount; } // 添加途经点 if (plan.wayPoints) { for (BMKPlanNode* tempNode in plan.wayPoints) { RouteAnnotation* item = [[RouteAnnotation alloc]init]; item = [[RouteAnnotation alloc]init]; item.coordinate = tempNode.pt; item.type = 5; item.title = tempNode.name; [_mapView addAnnotation:item]; } } //轨迹点 BMKMapPoint * temppoints = new BMKMapPoint[planPointCounts]; int i = 0; for (int j = 0; j < size; j++) { BMKDrivingStep* transitStep = [plan.steps objectAtIndex:j]; int k=0; for(k=0;k<transitStep.pointsCount;k++) { temppoints[i].x = transitStep.points[k].x; temppoints[i].y = transitStep.points[k].y; i++; } } // 通过points构建BMKPolyline BMKPolyline* polyLine = [BMKPolyline polylineWithPoints:temppoints count:planPointCounts]; [_mapView addOverlay:polyLine]; // 添加路线overlay delete []temppoints; } } - (void)onGetWalkingRouteResult:(BMKRouteSearch*)searcher result:(BMKWalkingRouteResult*)result errorCode:(BMKSearchErrorCode)error { NSArray* array = [NSArray arrayWithArray:_mapView.annotations]; [_mapView removeAnnotations:array]; array = [NSArray arrayWithArray:_mapView.overlays]; [_mapView removeOverlays:array]; if (error == BMK_SEARCH_NO_ERROR) { BMKWalkingRouteLine* plan = (BMKWalkingRouteLine*)[result.routes objectAtIndex:0]; int size = [plan.steps count]; int planPointCounts = 0; for (int i = 0; i < size; i++) { BMKWalkingStep* transitStep = [plan.steps objectAtIndex:i]; if(i==0){ RouteAnnotation* item = [[RouteAnnotation alloc]init]; item.coordinate = plan.starting.location; item.title = @"起点"; item.type = 0; [_mapView addAnnotation:item]; // 添加起点标注 }else if(i==size-1){ RouteAnnotation* item = [[RouteAnnotation alloc]init]; item.coordinate = plan.terminal.location; item.title = @"终点"; item.type = 1; [_mapView addAnnotation:item]; // 添加起点标注 } //添加annotation节点 RouteAnnotation* item = [[RouteAnnotation alloc]init]; item.coordinate = transitStep.entrace.location; item.title = transitStep.entraceInstruction; item.degree = transitStep.direction * 30; item.type = 4; [_mapView addAnnotation:item]; //轨迹点总数累计 planPointCounts += transitStep.pointsCount; } //轨迹点 BMKMapPoint * temppoints = new BMKMapPoint[planPointCounts]; int i = 0; for (int j = 0; j < size; j++) { BMKWalkingStep* transitStep = [plan.steps objectAtIndex:j]; int k=0; for(k=0;k<transitStep.pointsCount;k++) { temppoints[i].x = transitStep.points[k].x; temppoints[i].y = transitStep.points[k].y; i++; } } // 通过points构建BMKPolyline BMKPolyline* polyLine = [BMKPolyline polylineWithPoints:temppoints count:planPointCounts]; [_mapView addOverlay:polyLine]; // 添加路线overlay delete []temppoints; } }

3.路径规划


(责任编辑:ioter)

用户喜欢...

飞利浦照明携手百度,飞利浦 秀 全面接入度秘语音平台

全球照明领导企业飞利浦照明今日宣布,和中国领先科技企业百度(Baidu)签署合作协议。根据协议,百度旗下新一代...


基于微处理器的车载导航电子地图的设计和实现

车辆远程诊断仪的主要功能是导航。导航功能的重点是行车路线设计、自动车辆定位、综合信息服务、路径引导服务等。导航功能是GIS技术、通讯技术、嵌入式技术和GPS定位等技术相结合的综...


四维图新宣布与测绘院在高精地图车端加密算法研究上取得成果

在自动驾驶地图,即高精度地图(四维图新企业标准定义的应用于高度自动驾驶HAD地图)与车端保密插件加密算法研...


基于微处理器的车载导航电子地图的设计和实现

针对车辆远程终端仪中车载导航的硬件构成和GIS软件开发进行了探讨。给出了以Renesas Electronics公司SH7764微处理器为核...


百度VR新版本测评:高清超清模式如何让体验感翻番

每月15日前后,都能看到百度VR Android版本的应用更新,这次也不例外。此次更新,百度VR除了在首页新增了内容分类之...


Keep 5.0推出AR运动地图功能 成国内首个应用AR技术的APP

运动应用Keep发布全新5.0版本,立足“Stay Fun”强化运动社交属性和趣味体验。5.0版本中,Keep推出用户成长体系“...


百度物联网 天工物联网平台

天工是一站式全托管的物联网云平台,您可以通过天工轻松安全的将海量设备连接至云端,并在云端进行设备的管理,设备数据的处理计算、存储,可视化的展示与分析...


浪潮与百度联合发布ABC一体机 推进智慧计算的行业落地

在9月举办的2017 百度云智峰会 ABC SUMMIT 上,百度与浪潮共同发布了 ABC 一体机,双方将共同推动人工智能、大数据、云...


欧洲人工智能地图:超过400家人工智能(AI)公司

软件正掀起宁静的革命占领全世界,而启动“接管”的核心正是人工智能。欧洲创投网站Asgard,收集715家宣称为AI企业...


英特尔架构赋能百度云ABC愿景

每次来到中国,我都不禁感叹于创新带来的技术进步。在这里,每一个个人和企业都对变革充满热情。随着创新科技...