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

Google Car App Beta版改用MapWithContent,求地点列表与路线预览实现示例

Google Car App Beta版MapWithContent(地点列表+路线预览)实现示例

针对Google Car App Beta版弃用旧模板、改用MapWithContent新模板的场景,以下是地点列表结合路线预览的核心实现示例:

1. 基础模板结构配置

使用MapWithContentTemplate构建整合地图与侧边内容的核心容器,侧边栏用于放置地点列表,地图区域负责展示定位与路线:

val mapWithContentTemplate = MapWithContentTemplate.Builder()
    .setContentSidePanel(buildLocationListPanel()) // 传入地点列表侧边栏
    .setMapActionStrip(buildMapActionStrip()) // 配置地图操作按钮(如路线预览触发)
    .build()

2. 地点列表侧边栏实现

通过RecyclerView实现可交互的地点列表,点击列表项时触发地图定位或路线预览:

private fun buildLocationListPanel(): Panel {
    val recyclerView = RecyclerView(carContext).apply {
        adapter = LocationAdapter { selectedLoc ->
            // 选中地点后在地图标记位置
            carMapRenderer?.addMarker(selectedLoc.latLng, selectedLoc.name)
            // 触发路线预览
            startRoutePreview(selectedLoc)
        }
        layoutManager = LinearLayoutManager(carContext)
    }
    return Panel.Builder().setContent(recyclerView).build()
}

// 示例Adapter
class LocationAdapter(private val onItemClick: (Location) -> Unit) :
    RecyclerView.Adapter<LocationAdapter.ViewHolder>() {
    private val locations = mutableListOf<Location>()

    fun updateLocations(newLocations: List<Location>) {
        locations.clear()
        locations.addAll(newLocations)
        notifyDataSetChanged()
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view = LayoutInflater.from(parent.context)
            .inflate(R.layout.item_location, parent, false)
        return ViewHolder(view)
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val loc = locations[position]
        holder.nameTv.text = loc.name
        holder.addressTv.text = loc.address
        holder.itemView.setOnClickListener { onItemClick(loc) }
    }

    override fun getItemCount() = locations.size

    class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val nameTv: TextView = itemView.findViewById(R.id.tv_location_name)
        val addressTv: TextView = itemView.findViewById(R.id.tv_location_address)
    }
}

3. 路线预览功能集成

借助Google Maps Navigation SDK的预览模式,在MapWithContent的地图区域展示路线:

private fun startRoutePreview(destination: Location) {
    val navOptions = NavigationLauncherOptions.Builder()
        .setDestination(destination.latLng)
        .setOrigin(currentCarLocation.latLng)
        .setPreviewMode(true) // 启用预览模式,不进入正式导航
        .build()
    // 启动路线预览
    NavigationLauncher.launch(carContext, navOptions)
    // 在MapWithContent的地图上绘制路线
    carMapRenderer?.drawRoute(navOptions)
}

4. 生命周期与权限配置

  • 确保在Car App的Screen类中正确管理模板生命周期:
class MapWithContentScreen : Screen {
    private var carMapRenderer: CarMapRenderer? = null

    override fun onGetTemplate(): Template {
        return mapWithContentTemplate
    }

    override fun onStart() {
        super.onStart()
        carMapRenderer = CarMapRenderer(carContext, mapWithContentTemplate.mapController)
    }

    override fun onDestroy() {
        carMapRenderer?.cleanup()
        super.onDestroy()
    }
}
  • 在AndroidManifest.xml中配置必要权限:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="com.google.android.gms.permission.CAR_MAPS" />

以上示例基于Google Car App Beta版官方SDK实现,需确保项目依赖引入了最新Beta版SDK包。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.18 10:43:30