百度导航SDK驾车页采用提供底图,上层UI自定义的方式。在百度导航SDK中提供了一套完整的驾车页demo,开发者可以直接将这部分代码移到项目中使用。开发者如果需要自定义上层UI,也可以参考demo中的DrivePageViewController类实现。
目录
设置底图状态
通过BNMapViewManagerProtocol可以设置底图的中心点、比例尺、中心偏移值等状态。
BNMapViewStatus* mapViewStatus = [BNaviService_MapView getMapViewStatus]; mapViewStatus.level = 15; mapViewStatus.center.x = 113.949298; mapViewStatus.center.y = 22.530242; [BNaviService_MapView setMapViewStatus:mapViewStatus animated:NO];
路径规划
在开始驾车模式前,需要先根据起终点等进行路径规划。 路线规划可以参考 路线规划并发起专业导航
- (void)startNavi { // 节点数组 NSMutableArray *nodesArray = [[NSMutableArray alloc]initWithCapacity:2]; // 起点 BNRoutePlanNode *startNode = [[BNRoutePlanNode alloc] init]; startNode.pos = [[BNPosition alloc] init]; startNode.pos.x = 113.948222; startNode.pos.y = 22.549555; startNode.pos.eType = BNCoordinate_BaiduMapSDK; [nodesArray addObject:startNode]; // 终点 BNRoutePlanNode *endNode = [[BNRoutePlanNode alloc] init]; endNode.pos = [[BNPosition alloc] init]; endNode.pos.x = 113.940868; endNode.pos.y = 22.54647; endNode.pos.eType = BNCoordinate_BaiduMapSDK; [nodesArray addObject:endNode]; //关闭openURL,不想跳转百度地图可以设为YES [BNaviService_RoutePlan setDisableOpenUrl:YES]; [BNaviService_RoutePlan startNaviRoutePlan:BNRoutePlanMode_Recommend naviNodes:nodesArray time:nil delegete:self userInfo:nil]; }
开启定位
路径规划成功后,在回调函数中通过BNDriveRouteDataKey获取到路线信息,并且需要开启定位,开启定位才能显示车标。
/** * 算路成功回调 * * @param userInfo 用户信息 */ - (void)routePlanDidFinished:(NSDictionary*)userInfo { [self showRouteViewAll]; [self initMapSubView]; BNCarRouteModel *route = userInfo[BNDriveRouteDataKey]; if (route) { self.carRoute = route; [self routeSelectIndex:[BNaviService_RoutePlan GetCurrentSelectRouteIdx] needSelectRoute:NO]; [self resetUIFrame]; // [self resetUIFrame];需要在pageLoadedSuccess前,因为tabPanel中会用到tabPanel的高度 [self.contentView.tabPanel pageLoadedSuccess:self.carRoute]; [self.contentView.carDetailContentView refreshData:self.carRoute index:self.selectIndex]; } // 算路成功后需要打开定位才能显示车标 [BNaviService_DriveRoute startUpdateLocation]; }
渲染和移除底图
在驾车页的viewWillAppear和viewWillDisappear方法中渲染和移除底图。
- (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; self.navigationController.interactivePopGestureRecognizer.enabled = NO; [BNaviService_DriveRoute viewWillAppear:self.view]; } - (void)viewWillDisappear:(BOOL)animated { [super viewWillDisappear:animated]; self.navigationController.interactivePopGestureRecognizer.enabled = YES; [BNaviService_DriveRoute viewWillDisAppear:self.view]; }
路线全览
通过调用路线全览的接口进行全览路线,同时可以设置路线的四边边距。
BNMargin margin = {80, 20, self.view.height-self.radarSwitchView.y, 20}; [BNaviService_DriveRoute showRouteViewAll:margin animated:YES];
选择路线
通过设置路线序号来选择路线,同时高亮选择的路线。
[BNaviService_DriveRoute selectRouteAtIndex:selectIndex];
点选地图上的路线回调
在地图上点击了某条路线后会通过onHandleTouchRouteAtIndex函数回调。如果需要高亮该路线,要调用选择路线的接口来进行高亮。
/** 用户在地图上点击了某条路线(如果需要高亮该路线,要调用selectRouteAtIndex:接口) @param routeIndex 路线序号(从0开始) */ - (void)onHandleTouchRouteAtIndex:(NSUInteger)routeIndex { [BNaviService_DriveRoute selectRouteAtIndex:routeIndex]; }
从驾车页进入导航页
从驾车页进入导航页前需要先停止驾车页的定位监听,然后通过发起导航跳转到导航页。
[BNaviService_DriveRoute stopUpdateLocation]; [BNaviService_UI showPage:BNaviUI_NormalNavi delegate:self extParams:nil];
从导航页回到驾车页
从导航页回到驾车页后,需要在退出导航页的回调中将路线全览,并且开启驾车页的定位监听。
- (void)willExitPage:(BNaviUIType)pageType extraInfo:(NSDictionary*)extraInfo { [self showRouteViewAll]; [BNaviService_DriveRoute startUpdateLocation]; [self refreshRouteData]; }
获取当前路线信息
从导航页回到驾车页后路线可能发生变化,可以在退出导航页回到驾车页后获取当前路线信息重新渲染上层UI。获取当前路线信息可能为空,例如已经到达终点时,这时候可以退出驾车页。
- (void)refreshRouteData { BNCarRouteModel *route = [BNaviService_DriveRoute getCurrentCarRouteData]; if (route) { self.carRoute = route; [self routeSelectIndex:[BNaviService_RoutePlan GetCurrentSelectRouteIdx] needSelectRoute:NO]; [self resetUIFrame]; // [self resetUIFrame];需要在pageLoadedSuccess前,因为tabPanel中会用到tabPanel的高度 [self.contentView.tabPanel pageLoadedSuccess:self.carRoute]; [self.contentView.carDetailContentView refreshData:self.carRoute index:self.selectIndex]; } else { // 没获取到当前路线信息,有可能是已经到达终点,退出驾车页 [self.navigationController popViewControllerAnimated:YES]; } }
BNMapOverlayProtocol 图层管理器
图层管理器可以自定义导航中和轻导航的起点、终点、途径点的样式。 使用方式:
设置3D车标 需要将车标每5个角度都要生成一张图片。如5、10、15、
- (void)add3DCarLogoImages { NSMutableArray *images = [NSMutableArray array]; for (int i = 0; i < 360; i+=5) { NSString *imgName = [NSString stringWithFormat:@"%i", i]; UIImage *image = [UIImage imageNamed:imgName]; if (image) { [images addObject:image]; } else { NSLog(@"第%i张图片为空", i); } } [BNaviService_MapOverlay setDIY3DImageToMap:images imageType:BN_DIYImage_3DCar]; }
设置自定义类型图片。如果想替换原来途径点、起点、终点的icon。可以使用一下方式。 BN_DIYImageType 是需要替换的icon的类型。这个是设置所有的类型都会用这个icon替换。如果是设置成途径点,那么途径的所有icon都会设置成改图片。
[BNaviService_MapOverlay setDIYImageToMap:[UIImage imageNamed:@"Icon_arrive_bus"] imageType:BN_DIYImage_EndPoint];
自定义途经点。这个是单独 只设置途径点的icon。可以定义途径点每一个不一样的图片。
///存放图片的数组 NSArray<UIImage*> *customArr = @[图片1,图片2]; ///每一个数组对应的下标 0.1.2... NSMutableArray *customIndexArr = [NSMutableArray array]; for (int i=0; i<customArr.count; i++) { [customIndexArr addObject:@(i)]; } [BNaviService_MapOverlay setWayPointCustomImages:customArr indexes:customIndexArr];
清除自定义类型。将自己DIY自定义的图片全部清除。
/// 清除DIY起点。 [BNaviService_MapOverlay clearDIYImage:BN_DIYImage_StartPoint];
设置自定义图片隐藏和显示。
/// 隐藏起点icon。 [BNaviService_MapOverlay setDIYImageHidden:YES imageType:BN_DIYImage_StartPoint];