初始化使用方 SDK
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
在开始执行这些程序之前,请确保您已按照前几部分中的说明启用相应服务和 Consumer SDK。
如需初始化 Consumer SDK,请按以下步骤操作:
- 获取
ConsumerAPI
实例
- 初始化 Maps SDK 以请求首选渲染器
获取 ConsumerApi
实例
如需使用 Consumer SDK,您的应用需要异步初始化单例 ConsumerApi
。初始化方法采用 AuthTokenFactory
类,以便在必要时为用户生成新的 JWT 令牌。
providerId
是您的 Google Cloud 项目的项目 ID。如需详细了解如何创建 Fleet Engine 项目,请参阅 Fleet Engine 指南中的创建 Fleet Engine 项目。
您的应用应实现 AuthTokenFactory
(如消费者 SDK 身份验证中所述)。
Java
Task<ConsumerApi> consumerApiTask = ConsumerApi.initialize(
this, "myProviderId", authTokenFactory);
consumerApiTask.addOnSuccessListener(
consumerApi -> this.consumerApi = consumerApi);
Kotlin
val consumerApiTask =
ConsumerApi.initialize(this, "myProviderId", authTokenFactory)
consumerApiTask?.addOnSuccessListener { consumerApi: ConsumerApi ->
this@YourActivity.consumerApi = consumerApi
}
Maps SDK 和地图渲染器
Consumer SDK v2.0.0 及更高版本支持 Maps SDK for Android v18.1.0 及更高版本。下表总结了各个 Maps SDK 版本的默认渲染器以及两种渲染器的支持情况。如果可能,请使用最新的渲染器。
如果您必须使用旧版渲染程序,请使用 MapsInitializer.initialize()
明确指定。
Maps SDK 版本 |
支持最新渲染器 |
支持旧版渲染器 |
默认渲染器 |
V18.1.0 及更低版本 |
是 |
是 |
旧版* |
V18.2.0 |
是 |
是 |
最新 |
* 随着新版地图渲染程序的推出,“最新”渲染程序将成为默认渲染程序。
如果您必须使用首选渲染器,请在 OnMapsSdkInitializedCallback
返回结果后运行所有界面渲染操作。界面渲染操作包括以下操作:
如果您在收到 OnMapsSdkInitializedCallback
结果后未运行这些操作,Maps SDK 将不会分配您的首选渲染器,而是由默认渲染器渲染地图视图。
先初始化 Maps SDK,然后再初始化 Consumer SDK
在 Application
或启动 Activity
类中,调用 MapsInitializer.initialize()
在初始化 Consumer SDK 之前,等待渲染器请求结果。
如需了解详情,请参阅以下示例。
Java
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
initViews();
MapsInitializer.initialize(getApplicationContext(), Renderer.LATEST,
new OnMapsSdkInitializedCallback() {
@Override
public void onMapsSdkInitialized(Renderer renderer) {
switch (renderer) {
case LATEST:
Log.i("maps_renderer", "LATEST renderer");
break;
case LEGACY:
Log.i("maps_renderer", "LEGACY renderer");
break;
}
initializeConsumerSdk();
}
});
}
Kotlin
fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.main)
initViews()
MapsInitializer.initialize(
getApplicationContext(), Renderer.LATEST,
object : OnMapsSdkInitializedCallback() {
fun onMapsSdkInitialized(renderer: Renderer?) {
when (renderer) {
LATEST -> Log.i("maps_renderer", "LATEST renderer")
LEGACY -> Log.i("maps_renderer", "LEGACY renderer")
}
initializeConsumerSdk()
}
})
}
SSL/TLS 相关注意事项
在内部,Consumer SDK 实现使用 SSL/TLS 与 Fleet Engine 服务安全地通信。Android API 版本 23 或更低版本可能需要 SecurityProvider
补丁才能与服务器通信。如需详细了解如何在 Android 中使用 SSL,请参阅 Security GMS Provider。
本文还包含用于修补安全提供程序的代码示例。
后续步骤
设置地图
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-08-31。
[null,null,["最后更新时间 (UTC):2025-08-31。"],[[["\u003cp\u003eThe Consumer SDK requires initialization with a provider ID and authentication token factory before use.\u003c/p\u003e\n"],["\u003cp\u003eMaps SDK v18.1.0 or later is required, with the latest renderer preferred for optimal performance.\u003c/p\u003e\n"],["\u003cp\u003eBefore initializing the Consumer SDK, initialize Maps SDK and specify your preferred renderer to ensure proper map rendering.\u003c/p\u003e\n"],["\u003cp\u003eSecure communication is handled via SSL/TLS, and Android API versions 23 or earlier may require a security provider patch.\u003c/p\u003e\n"]]],["Before initialization, ensure the required services and Consumer SDK are enabled. To initialize the Consumer SDK, first, get the `ConsumerApi` instance using `ConsumerApi.initialize()`, providing the application context, project ID, and an `AuthTokenFactory` for JWT token generation. Next, initialize the Maps SDK via `MapsInitializer.initialize()` to specify the preferred renderer (latest or legacy) and wait for `OnMapsSdkInitializedCallback` before any UI rendering like `GoogleMapView`. Note that the Consumer SDK uses SSL/TLS for secure communication.\n"],null,["Before starting these procedures, make sure you have enabled the appropriate\nservices and the Consumer SDK as described in earlier sections.\n\nTo initialize the Consumer SDK, follow these steps:\n\n1. [Get the `ConsumerAPI` instance](#getinstance)\n2. [Initialize Maps SDK to request for preferred renderer](#init-maps)\n\nGet the `ConsumerApi` instance\n\nTo use the Consumer SDK, your app needs to initialize the singleton\n`ConsumerApi` asynchronously. The initialization method takes the\n`AuthTokenFactory` class to generate new JWT tokens for the user when necessary.\n\nThe `providerId` is the **Project ID** of your Google Cloud Project. For more\ninformation about creating a Fleet Engine project, see\n[Create your Fleet Engine project](/maps/documentation/mobility/fleet-engine/essentials/set-up-fleet/create-project) in the Fleet Engine guide.\n\nYour app should implement the `AuthTokenFactory` as described in [Consumer SDK\nAuthentication](/maps/documentation/mobility/journey-sharing/on-demand/android/auth-sdk). \n\nJava \n\n Task\u003cConsumerApi\u003e consumerApiTask = ConsumerApi.initialize(\n this, \"myProviderId\", authTokenFactory);\n\n consumerApiTask.addOnSuccessListener(\n consumerApi -\u003e this.consumerApi = consumerApi);\n\nKotlin \n\n val consumerApiTask =\n ConsumerApi.initialize(this, \"myProviderId\", authTokenFactory)\n\n consumerApiTask?.addOnSuccessListener { consumerApi: ConsumerApi -\u003e\n this@YourActivity.consumerApi = consumerApi\n }\n\nMaps SDK and maps renderers\n\nThe Consumer SDK v2.0.0 and later support the Maps SDK for Android v18.1.0 and\nlater. The following table summarizes the default renderer by Maps SDK version\nand the supportability of both renderers. If possible, use the latest renderer.\nIf you must use the legacy renderer, explicitly specify it using\n[`MapsInitializer.initialize()`](https://developers.google.com/android/reference/com/google/android/gms/maps/MapsInitializer).\n\n| Maps SDK version | Supports the latest renderer | Supports the legacy renderer | Default renderer |\n|-------------------|------------------------------|------------------------------|------------------|\n| V18.1.0 and below | Yes | Yes | Legacy\\* |\n| V18.2.0 | Yes | Yes | Latest |\n\n\\* With the rollout of [new Maps Renderer](https://developers.google.com/maps/documentation/android-sdk/renderer#rollout_schedule), the Latest renderer will be the\ndefault.\n\nIf you must use a preferred renderer, run all UI-rendering operations\n**after** `OnMapsSdkInitializedCallback` returns a result. UI-rendering\noperations include the following operations:\n\n- Inflating a view that contains `GoogleMapView` or `ConsumerMapView`.\n\n- Placing markers on `ConsumerMapView`.\n\nIf you don't run these operations after receiving the\n`OnMapsSdkInitializedCallback` result, the Maps SDK doesn't allocate your\npreferred renderer and the map view is instead rendered by the default renderer.\n\nInitialize Maps SDK before initializing the Consumer SDK\n\n1. In your `Application` or start-up `Activity` class, call\n [MapsInitializer.initialize()](https://developers.google.com/maps/documentation/android-sdk/renderer)\n\n2. Wait for the renderer request result before initializing the Consumer SDK.\n\nSee the following examples for details. \n\nJava \n\n @Override\n protected void onCreate(Bundle savedInstanceState) {\n super.onCreate(savedInstanceState);\n setContentView(R.layout.main);\n initViews();\n\n MapsInitializer.initialize(getApplicationContext(), Renderer.LATEST,\n new OnMapsSdkInitializedCallback() {\n @Override\n public void onMapsSdkInitialized(Renderer renderer) {\n switch (renderer) {\n case LATEST:\n Log.i(\"maps_renderer\", \"LATEST renderer\");\n break;\n case LEGACY:\n Log.i(\"maps_renderer\", \"LEGACY renderer\");\n break;\n }\n\n initializeConsumerSdk();\n }\n });\n }\n\nKotlin \n\n fun onCreate(savedInstanceState: Bundle?) {\n super.onCreate(savedInstanceState)\n setContentView(R.layout.main)\n initViews()\n\n MapsInitializer.initialize(\n getApplicationContext(), Renderer.LATEST,\n object : OnMapsSdkInitializedCallback() {\n fun onMapsSdkInitialized(renderer: Renderer?) {\n when (renderer) {\n LATEST -\u003e Log.i(\"maps_renderer\", \"LATEST renderer\")\n LEGACY -\u003e Log.i(\"maps_renderer\", \"LEGACY renderer\")\n }\n initializeConsumerSdk()\n }\n })\n }\n\nNotes on SSL/TLS\n\nInternally, the Consumer SDK implementation uses SSL/TLS to communicate securely\nwith the Fleet Engine service. Android API versions 23 or earlier may require a\n`SecurityProvider` patch to communicate with the server. For more information\nabout working with SSL in Android, see [Security GMS\nProvider](https://developer.android.com/training/articles/security-gms-provider).\nThe article also contains code samples for patching the security provider.\n\nWhat's next\n\n[Set up a map](/maps/documentation/mobility/journey-sharing/on-demand/android/create-ui)"]]