初始化 Consumer 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
,如「Consumer 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 2.0.0 以上版本支援 Maps SDK for Android 18.1.0 以上版本。下表摘要說明各個 Maps SDK 版本的預設轉譯器,以及這兩種轉譯器的支援情形。盡可能使用最新版算繪器。
如果必須使用舊版轉譯器,請使用 MapsInitializer.initialize()
明確指定。
Maps SDK 版本 |
支援最新轉譯器 |
支援舊版轉譯器 |
預設算繪器 |
V18.1.0 以下版本 |
是 |
是 |
舊版* |
V18.2.0 |
是 |
是 |
最新 |
* 新版地圖轉譯器推出後,系統會預設使用最新版轉譯器。
如必須使用偏好的轉譯器,請在 OnMapsSdkInitializedCallback
傳回結果「後」執行所有 UI 轉譯作業。UI 算繪作業包括下列作業:
如果收到 OnMapsSdkInitializedCallback
結果後未執行這些作業,地圖 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」。本文也包含修補安全性供應商的程式碼範例。
後續步驟
設定地圖
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2025-09-04 (世界標準時間)。
[null,null,["上次更新時間:2025-09-04 (世界標準時間)。"],[[["\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)"]]