使用百度导航SDK提供的专业导航服务之前,需要先根据起终点等进行路线规划,路线规划成功后,才能正常使用专业导航服务。
路线规划偏好
/** * 路线计算类型 */ typedef enum { BNRoutePlanMode_Invalid = 0X00000000 , /**< 无效值 */ BNRoutePlanMode_Recommend = 0X00000001 , /**< 推荐 */ BNRoutePlanMode_NoHeighWay = 0X00000004 , /**< 不走高速 */ BNRoutePlanMode_LessToll = 0X00000008 , /**< 少收费 */ BNRoutePlanMode_LessJam = 0X00000010 , /**< 躲避拥堵 */ BNRoutePlanMode_SaveTime = 0X00000100 , /**< 时间优先 */ BNRoutePlanMode_MainRoad = 0X00000200 , /**< 高速优先 */ }BNRoutePlanMode;
示例代码
路径规划如下:
- (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]; }
算路成功后,在回调函数中发起导航,如下:
//算路成功回调 -(void)routePlanDidFinished:(NSDictionary *)userInfo { NSLog(@"算路成功"); //路径规划成功,开始导航 [BNaviService_UI showPage:BNaviUI_NormalNavi delegate:self extParams:nil]; }
下图为发起导航后的界面图:
BNRoutePlanManagerProtocol 路线规划并发起专业导航
当前的这个Protocol主要负责算路模块的业务。新增两个方法:
使用方式 选路,注意,这是一个异步的过程。
///最主要算路⽅法 /** * 发起算路 * * @param eMode 算路⽅式,定义⻅BNRoutePlanMode * @param naviNodes 算路节点数组,起点、途经点、终点按顺序排列,节点信息为BNRoutePlanNode结 构 * @param naviTime 发起算路时间,⽤于优化算路结果,可以为nil * @param delegate 算路委托,⽤于回调 * @param userInfo ⽤户需要传⼊的参数,货⻋导航算路需要传⼊BNaviTripTypeKey,值为 BN_NaviTypeTruck * ⽀持传⼊参数: * BNaviTripTypeKey:NSNumber *, 可选,BN_NaviType类型,不传默认为驾⻋。 * (1)货⻋,传@(BN_NaviTypeTruck) * (2)驾⻋,传@(BN_NaviTypeReal) * (3)摩托⻋,传@(BN_NaviTypeMoto) */ - (void)startNaviRoutePlan:(BNRoutePlanMode)eMode naviNodes:(NSArray*)naviNodes time:(BNaviCalcRouteTime*)naviTime delegete:(id<BNNaviRoutePlanDelegate>)delegate userInfo:(NSDictionary*)userInfo; ///导航默认选中第⼀条路线。路线最多3条。 -(BOOL)selectRouteIndex:(NSInteger)routeIndex; -(void)selectRouteIndex:(NSInteger)routeIndex { //选择第⼀条路线 导航默认选中第⼀条路线。路线最多3条。 [BNRoutePlanManager_Instance selectRouteIndex:routeIndex]; } -(BOOL)selectRouteIndex:(NSInteger)routeIndex completion:(dispatch_block_t)completion; ///算路完成后 1 -(void)routePlanDidFinished:(NSDictionary*)userInfo { BNCarRouteModel*routeModel = userInfo[BNDriveRouteDataKey]; //选择最后⼀条路线 NSInteger selectRouteIndex = routeModel.carRoutes.count -1; NSLog(@"路线条数 = %ld, 选择路线序号 = %ld",routeModel.carRoutes.count, selectRouteIndex); if(selectRouteIndex <0){ selectRouteIndex = 0; } [BNaviService_RoutePlan selectRouteIndex:selectRouteIndex completion:^{[BNaviService_RoutePlan showMultiRoute:NO];}]; }