Khởi chạy SDK người tiêu dùng
Sử dụng bộ sưu tập để sắp xếp ngăn nắp các trang
Lưu và phân loại nội dung dựa trên lựa chọn ưu tiên của bạn.
Trước khi bắt đầu các quy trình này, hãy đảm bảo bạn đã bật các dịch vụ phù hợp và Consumer SDK như mô tả trong các phần trước.
Để khởi chạy Consumer SDK, hãy làm theo các bước sau:
- Lấy thực thể
ConsumerAPI
- Khởi chạy Maps SDK để yêu cầu trình kết xuất ưu tiên
Nhận thực thể ConsumerApi
Để sử dụng Consumer SDK, ứng dụng của bạn cần khởi động singleton ConsumerApi
không đồng bộ. Phương thức khởi tạo lấy lớp AuthTokenFactory
để tạo mã thông báo JWT mới cho người dùng khi cần.
providerId
là Mã dự án của Dự án trên Google Cloud. Để biết thêm thông tin về cách tạo dự án Fleet Engine, hãy xem phần Tạo dự án Fleet Engine trong hướng dẫn về Fleet Engine.
Ứng dụng của bạn phải triển khai AuthTokenFactory
như mô tả trong phần Xác thực SDK người dùng.
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
}
SDK Maps và trình kết xuất bản đồ
Consumer SDK phiên bản 2.0.0 trở lên hỗ trợ Maps SDK cho Android phiên bản 18.1.0 trở lên. Bảng sau đây tóm tắt trình kết xuất mặc định theo phiên bản Maps SDK và khả năng hỗ trợ của cả hai trình kết xuất. Nếu có thể, hãy sử dụng trình kết xuất mới nhất.
Nếu phải dùng trình kết xuất cũ, hãy chỉ định rõ trình kết xuất đó bằng cách dùng MapsInitializer.initialize()
.
Phiên bản Maps SDK |
Hỗ trợ trình kết xuất mới nhất |
Hỗ trợ trình kết xuất cũ |
Trình kết xuất mặc định |
Phiên bản 18.1.0 trở xuống |
Có |
Có |
Cũ* |
V18.2.0 |
Có |
Có |
Mới nhất |
* Khi Trình kết xuất Maps mới ra mắt, Trình kết xuất mới nhất sẽ là trình kết xuất mặc định.
Nếu bạn phải sử dụng một trình kết xuất ưu tiên, hãy chạy tất cả các thao tác kết xuất giao diện người dùng sau khi OnMapsSdkInitializedCallback
trả về kết quả. Các thao tác kết xuất giao diện người dùng bao gồm những thao tác sau:
Nếu bạn không chạy các thao tác này sau khi nhận được kết quả OnMapsSdkInitializedCallback
, Maps SDK sẽ không phân bổ trình kết xuất mà bạn muốn và thay vào đó, chế độ xem bản đồ sẽ được trình kết xuất mặc định kết xuất.
Khởi chạy Maps SDK trước khi khởi chạy Consumer SDK
Trong lớp Application
hoặc lớp khởi động Activity
, hãy gọi MapsInitializer.initialize()
Đợi kết quả yêu cầu của trình kết xuất trước khi khởi chạy Consumer SDK.
Hãy xem các ví dụ sau đây để biết thông tin chi tiết.
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()
}
})
}
Lưu ý về SSL/TLS
Về nội bộ, việc triển khai Consumer SDK sử dụng SSL/TLS để giao tiếp an toàn với dịch vụ Fleet Engine. Các phiên bản Android API 23 trở về trước có thể yêu cầu bản vá SecurityProvider
để giao tiếp với máy chủ. Để biết thêm thông tin về cách sử dụng SSL trong Android, hãy xem Nhà cung cấp GMS bảo mật.
Bài viết này cũng chứa các mẫu mã để vá nhà cung cấp dịch vụ bảo mật.
Bước tiếp theo
Thiết lập bản đồ
Trừ phi có lưu ý khác, nội dung của trang này được cấp phép theo Giấy phép ghi nhận tác giả 4.0 của Creative Commons và các mẫu mã lập trình được cấp phép theo Giấy phép Apache 2.0. Để biết thông tin chi tiết, vui lòng tham khảo Chính sách trang web của Google Developers. Java là nhãn hiệu đã đăng ký của Oracle và/hoặc các đơn vị liên kết với Oracle.
Cập nhật lần gần đây nhất: 2025-09-04 UTC.
[[["Dễ hiểu","easyToUnderstand","thumb-up"],["Giúp tôi giải quyết được vấn đề","solvedMyProblem","thumb-up"],["Khác","otherUp","thumb-up"]],[["Thiếu thông tin tôi cần","missingTheInformationINeed","thumb-down"],["Quá phức tạp/quá nhiều bước","tooComplicatedTooManySteps","thumb-down"],["Đã lỗi thời","outOfDate","thumb-down"],["Vấn đề về bản dịch","translationIssue","thumb-down"],["Vấn đề về mẫu/mã","samplesCodeIssue","thumb-down"],["Khác","otherDown","thumb-down"]],["Cập nhật lần gần đây nhất: 2025-09-04 UTC."],[[["\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)"]]