You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何基于Heremaps Navigation SDK开发Android Auto导航应用?

实现Android Auto中的HERE Maps导航功能

前置准备

  • 确认项目已集成HERE Maps Navigation SDK,且完成Android Auto基础配置:在Manifest中添加车载特性声明、权限(ACCESS_FINE_LOCATION、ACCESS_COARSE_LOCATION、INTERNET等),并配置有效的HERE API密钥。
  • 确保已初始化HERE地图引擎(MapEngine),能正常渲染地图。

核心导航集成步骤

1. 初始化导航引擎

关联已初始化的地图引擎,启动导航管理器:

val navigationManager = NavigationManager.getInstance()
navigationManager.setContext(applicationContext)
navigationManager.setMapEngine(mapEngine)
navigationManager.start()

2. 计算并启动导航路线

构建路线规划请求,获取路线后启动导航:

// 构建起点(当前位置)和终点
val startWaypoint = Waypoint.createFromCurrentLocation()
val endWaypoint = Waypoint.createFromGeoCoordinate(GeoCoordinate(39.9042, 116.4074)) // 示例坐标

val routePlan = RoutePlan().apply {
    addWaypoint(startWaypoint)
    addWaypoint(endWaypoint)
    routeOptions = RouteOptions().apply {
        transportMode = RouteOptions.TransportMode.CAR
        routeType = RouteOptions.Type.FASTEST
        enableTraffic = true // 启用路况避让
    }
}

// 请求路线并启动导航
navigationManager.calculateRoute(routePlan, object : RouteManager.CalculateRouteCallback {
    override fun onCalculateRouteFinished(error: RouteManager.Error?, routes: MutableList<Route>?) {
        error?.run {
            // 处理路线计算失败逻辑,比如弹窗提示
            return
        }
        routes?.firstOrNull()?.let { targetRoute ->
            navigationManager.startNavigation(targetRoute)
        }
    }
})

3. 监听导航事件,适配Android Auto UI

注册导航事件监听器,获取转向指令、道路名称更新、到达提醒等信息,同步到Android Auto界面:

navigationManager.addNavigationManagerListener(object : NavigationManager.NavigationManagerListener {
    override fun onManeuverEvent(maneuverEvent: ManeuverEvent?) {
        maneuverEvent?.maneuver?.let { maneuver ->
            // 获取转向指令文本和类型,比如"前方500米右转"
            val instruction = maneuver.instruction
            val maneuverType = maneuver.type
            // 更新Android Auto的导航提示UI(比如显示转向箭头、指令文本)
        }
    }

    override fun onCurrentRoadNameChanged(currentRoadName: String?) {
        // 更新车载界面显示的当前道路名称
    }

    override fun onDestinationReached() {
        // 触发到达逻辑:停止导航、显示到达提示
        navigationManager.stopNavigation()
    }

    override fun onRouteUpdated(route: Route?) {
        // 路线因路况重新计算时,更新导航路线
    }
})

Android Auto专属适配

1. 实现车载导航服务

创建继承自CarAppService的服务,用于管理Android Auto的导航会话:

class AutoNavigationService : CarAppService() {
    override fun onCreateSession(): Session {
        return NavigationSession()
    }

    inner class NavigationSession : Session() {
        override fun onCreateCarAppWindow(carContext: CarContext): CarAppWindow {
            val window = super.onCreateCarAppWindow(carContext)
            // 创建适配车载的导航视图,关联HERE地图控件
            val autoNavView = NavigationView(carContext).apply {
                // 绑定导航管理器,同步地图视角与导航路线
                bindNavigationManager(navigationManager)
            }
            window.setContentView(autoNavView)
            return window
        }
    }
}

2. 配置Manifest声明

在Manifest中注册车载服务,声明导航类别:

<service
    android:name=".AutoNavigationService"
    android:exported="true">
    <intent-filter>
        <action android:name="androidx.car.app.CarAppService" />
        <category android:name="androidx.car.app.category.NAVIGATION" />
    </intent-filter>
</service>

3. 适配车载UI规范

使用Android Auto提供的CarText、CarIcon组件展示导航信息,确保提示简洁、符合驾驶场景:

  • 转向提示优先用图标+短文本,避免冗长描述
  • 剩余距离/时间放在显眼位置,不遮挡地图核心区域

测试验证

  • 使用Android Auto模拟器或真实车载设备连接测试,验证导航启动、路线指引、事件提示的完整性
  • 测试极端场景:无网络时的离线导航、路线重新计算、目的地到达逻辑

内容的提问来源于stack exchange,提问作者BalaSubramaniam

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.18 03:05:20