实时导航
实时导航,也可以称为GPS导航,是通过连续的GPS定位信息来驱动进行的导航,用于实际的导航过程。在路线规划成功以后(驾车或步行),就可以开始导航了。示例代码如下:
- (完整代码详见 SDKDemo 工程的 MBNaviController.m)
- // 实时导航
- if (naviMapView == nil) {
- // 地图不存在
- naviMapView=[[MBMapView alloc]initWithFrame:(0,120.0,kMainScreenSizeWidth, kMainScreenSizeHeight - 120.0 - 55.0)];
- [self.view addSubview:naviMapView];
- // 创建车标图层
- MBModelOverlay *car = [[MBModelOverlay alloc] initWithFilePath:@"res/red_car.obj"];
- carHead = MBNaviHeadNorthUp;
- }
- // 初始化导航模块
- MBNaviSession *naviSession = [MBNaviSession sharedInstance];
- naviSession.enableSound = YES;
- naviSession.delegate = self;
- // 路线计算完成的回调函数
- - (void)naviSessionResult:(MBRouteCollection *)routes {
- // 算路完成
- // 得到routes集合,此集合中的routeBases,在发起导航获取数据时需用到
- for (MBOverlay *overlay in naviMapView.overlays) {
- [naviMapView removeOverlay:overlay];
- }
- if (routes.routeBases.count > 0) {
- MBRouteOverlay *routeOverlay = nil;
- for (MBRouteBase *base in routes.routeBases) {
- routeOverlay = [[MBRouteOverlay alloc] initWithRoute:base.getRouteBase];
- routeOverlay.clickEnable = YES;
- [naviMapView addOverlay:routeOverlay];
- }
- }
- }
- - (void)naviSessionTracking:(MBNaviSessionData *)sData {
- NSLog(@"当前车的位置:%d,%d",sData.carPos.x,sData.carPos.y);
- NSLog(@"当前车头朝向:%ld",(long)sData.carOri);
- NSLog(@"当前建议比例尺:%ld",(long)sData.suggestedMapScale);
- NSLog(@"是否有下一个转弯:%@,还有%ld米转弯",sData.hasTurn?@"是":@"没有",(long)sData.turnIconDistance);
- NSLog(@"当前转向标ID:%ld",(long)sData.turnIcon);
- NSLog(@"转弯之后的道路名称:%@",sData.nextRoadName);
- NSLog(@"当前所在道路名称:%@",sData.roadName);
- NSLog(@"\n");
- }