构建地图注点应用

本指南详细介绍了汽车应用库的不同功能,您可以使用这些功能实现地图注点 (POI) 的功能。

在清单中声明类别支持

应用需要在其 CarAppService 的 intent 过滤器中声明 androidx.car.app.category.POI 汽车应用类别

以下示例展示了如何声明应用类别:

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

访问地图模板

应用可以访问 PlaceListMapTemplate,该模板专门用于在由主机渲染的地图上显示一系列地图注点。

若要访问此模板,应用需要在其 AndroidManifest.xml 文件中声明 androidx.car.app.MAP_TEMPLATES 权限:

<uses-permission android:name="androidx.car.app.MAP_TEMPLATES"/>

刷新 PlaceListMapTemplate 内容

您可以允许驾驶员在浏览使用 PlaceListMapTemplate 构建的地点列表时通过点按按钮来刷新内容。实现 OnContentRefreshListener 接口的 onContentRefreshRequested 方法并使用 PlaceListMapTemplate.Builder.setOnContentRefreshListener 在模板上设置监听器,即可启用列表刷新功能。

以下代码段展示了如何在模板上设置监听器:

Kotlin

PlaceListMapTemplate.Builder()
    ...
    .setOnContentRefreshListener {
        // Execute any desired logic
        ...
        // Then call invalidate() so onGetTemplate() is called again
        invalidate()
    }
    .build()

Java

new PlaceListMapTemplate.Builder()
        ...
        .setOnContentRefreshListener(() -> {
            // Execute any desired logic
            ...
            // Then call invalidate() so onGetTemplate() is called again
            invalidate();
        })
        .build();

仅当监听器具有值时,PlaceListMapTemplate 的页眉中才会显示刷新按钮。

当用户点击刷新按钮时,系统会调用您的 OnContentRefreshListener 实现的 onContentRefreshRequested 方法。在 onContentRefreshRequested 中,调用 Screen.invalidate 方法。主机随后回调应用的 Screen.onGetTemplate 方法,以检索包含已刷新内容的模板。如需详细了解如何刷新模板,请参阅刷新模板的内容。只要 onGetTemplate 返回的下一个模板属于同一类型,系统就会将其视为一次刷新,而不会将其计入模板配额。

使用与应用有关的 Action 与 Google 助理相集成

使用 Google 助理为您的地图注点应用启用语音功能,让用户能够通过语音询问来搜索地图注点,例如询问“Ok Google,在 ExampleApp 上查找附近的充电站”。如需了解详细说明,请参阅适用于汽车的与应用有关的 Action