示例中心
功能在线体验
menu 导航 closed 关闭

高德 开发 iOS 导航SDK 开发指南 专业导航 实时导航与模拟导航

更新时间:2020年10月21日

实时导航

实时导航,就是通过连续的定位信息来驱动的导航,应用于实际的导航过程。在路线规划成功后,就可以开始实时导航了。我们提供了一个默认的导航界面AMapNaviDriveView的示意图,如下图所示。 

使用说明

实现实时导航的步骤如下:

第1步,创建AMapNaviDriveView

- (void)initDriveView {
    if (self.driveView == nil) {
        self.driveView = [[AMapNaviDriveView alloc] initWithFrame:self.view.bounds];
        self.driveView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
        [self.driveView setDelegate:self];
        
        [self.view addSubview:self.driveView];
    }
}
func initDriveView() {
   driveView = AMapNaviDriveView(frame: view.bounds)
   driveView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
   driveView.delegate = self
   
   view.addSubview(driveView)
}

第2步,创建AMapNaviDriveManager

构造AMapNaviDriveManager的之后,将AMapNaviDriveView添加为导航数据的Representable,使其可以接收到导航实时数据。 

- (void)initDriveManager {
    [[AMapNaviDriveManager sharedInstance] setDelegate:self];   
    
    //将driveView添加为导航数据的Representative,使其可以接收到导航诱导数据
    [[AMapNaviDriveManager sharedInstance] addDataRepresentative:self.driveView]
}
func initDriveManager() {
   AMapNaviDriveManager.sharedInstance().delegate = self
   
   AMapNaviDriveManager.sharedInstance().allowsBackgroundLocationUpdates = true
   AMapNaviDriveManager.sharedInstance().pausesLocationUpdatesAutomatically = false
   
   //将driveView添加为导航数据的Representative,使其可以接收到导航诱导数据
   AMapNaviDriveManager.sharedInstance().addDataRepresentative(driveView)
}

第3步,进行路线规划

具体参考:驾车路线规划步行路线规划骑行路线规划

第4步,开始实时导航

在路线规划成功的回调函数中,开启实时导航。 

- (void)driveManager:(AMapNaviDriveManager *)driveManager onCalculateRouteSuccessWithType:(AMapNaviRoutePlanType)type {
    NSLog(@"onCalculateRouteSuccess");
 
    //算路成功后开始GPS导航
    [[AMapNaviDriveManager sharedInstance] startGPSNavi];
}
func driveManager(_ driveManager: AMapNaviDriveManager, onCalculateRouteSuccessWith type: AMapNaviRoutePlanType) {
   NSLog("onCalculateRouteSuccess");
    
    //算路成功后开始GPS导航
   AMapNaviDriveManager.sharedInstance().startGPSNavi()
}

第5步,当退出页面的时候,销毁AMapNaviDriveManager单例

- (void)dealloc {
    [[AMapNaviDriveManager sharedInstance] stopNavi];
    [[AMapNaviDriveManager sharedInstance] removeDataRepresentative:self.driveView];
    [[AMapNaviDriveManager sharedInstance] setDelegate:nil];
    
    BOOL success = [AMapNaviDriveManager destroyInstance];
    NSLog(@"单例是否销毁成功 : %d",success); 
}
deinit {
    AMapNaviDriveManager.sharedInstance().stopNavi()
    AMapNaviDriveManager.sharedInstance().removeDataRepresentative(driveView)
    AMapNaviDriveManager.sharedInstance().delegate = nil
        
    let success = AMapNaviDriveManager.destroyInstance()
    NSLog("单例是否销毁成功 : \(success)")
        
}

模拟导航

模拟导航,仅用于室内模拟使用,目的是让您对导航功能有一些更直观的了解,比如预先了解既定路线的一些情况,如路况信息、电子眼信息等。注意:不要将模拟导航作为实际导航展示。

模拟导航的实现步骤与实时导航基本一致,区别就是在路径规划成功的回调函数中调用startEmulatorNavi方法开启模拟导航。 

- (void)driveManager:(AMapNaviDriveManager *)driveManager onCalculateRouteSuccessWithType:(AMapNaviRoutePlanType)type;
{
    NSLog(@"onCalculateRouteSuccess");
 
    //算路成功后开始模拟导航
    [[AMapNaviDriveManager sharedInstance] startEmulatorNavi];
}
func driveManager(_ driveManager: AMapNaviDriveManager, onCalculateRouteSuccessWith type: AMapNaviRoutePlanType) 
{
   NSLog("onCalculateRouteSuccess");
    
   //算路成功后开始模拟导航
   AMapNaviDriveManager.sharedInstance().startEmulatorNavi()
}
新用户
福利
示例
中心
常见
问题