เริ่มต้น SDK ของผู้บริโภค
จัดทุกอย่างให้เป็นระเบียบอยู่เสมอด้วยคอลเล็กชัน
บันทึกและจัดหมวดหมู่เนื้อหาตามค่ากำหนดของคุณ
ก่อนเริ่มกระบวนการเหล่านี้ โปรดตรวจสอบว่าคุณได้เปิดใช้บริการที่เหมาะสมและ Consumer SDK ตามที่อธิบายไว้ในส่วนก่อนหน้า
หากต้องการเริ่มต้น SDK สำหรับผู้บริโภค ให้ทำตามขั้นตอนต่อไปนี้
- รับ
ConsumerAPI
อินสแตนซ์
- เริ่มต้น Maps SDK เพื่อขอตัวแสดงผลที่ต้องการ
รับอินสแตนซ์ ConsumerApi
หากต้องการใช้ Consumer SDK แอปของคุณต้องเริ่มต้นใช้งาน Singleton
ConsumerApi
แบบไม่พร้อมกัน เมธอดการเริ่มต้นจะใช้คลาส
AuthTokenFactory
เพื่อสร้างโทเค็น JWT ใหม่สำหรับผู้ใช้เมื่อจำเป็น
providerId
คือรหัสโปรเจ็กต์ของโปรเจ็กต์ Google Cloud ดูข้อมูลเพิ่มเติม
เกี่ยวกับการสร้างโปรเจ็กต์ 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 เวอร์ชัน 2.0.0 ขึ้นไปรองรับ Maps SDK สำหรับ Android เวอร์ชัน 18.1.0 ขึ้นไป
ตารางต่อไปนี้สรุปโปรแกรมแสดงผลเริ่มต้นตามเวอร์ชัน Maps SDK
และความสามารถในการรองรับโปรแกรมแสดงผลทั้ง 2 รายการ หากเป็นไปได้ ให้ใช้โปรแกรมแสดงผลเวอร์ชันล่าสุด
หากคุณต้องใช้โปรแกรมแสดงผลเดิม ให้ระบุอย่างชัดเจนโดยใช้
MapsInitializer.initialize()
เวอร์ชัน Maps SDK |
รองรับโปรแกรมแสดงผลล่าสุด |
รองรับโปรแกรมแสดงผลเวอร์ชันเดิม |
โปรแกรมแสดงผลเริ่มต้น |
V18.1.0 และต่ำกว่า |
ใช่ |
ใช่ |
เดิม* |
V18.2.0 |
ใช่ |
ใช่ |
ล่าสุด |
* เมื่อเปิดตัว โปรแกรมแสดงผล Maps ใหม่ โปรแกรมแสดงผลล่าสุดจะเป็นค่าเริ่มต้น
หากต้องใช้โปรแกรมแสดงผลที่ต้องการ ให้เรียกใช้การดำเนินการแสดงผล UI ทั้งหมดหลังจาก OnMapsSdkInitializedCallback
แสดงผลลัพธ์ การดำเนินการที่เกี่ยวข้องกับการแสดงผล UI
ได้แก่ การดำเนินการต่อไปนี้
หากคุณไม่เรียกใช้การดำเนินการเหล่านี้หลังจากได้รับOnMapsSdkInitializedCallback
ผลลัพธ์ Maps SDK จะไม่จัดสรรโปรแกรมแสดงผลที่คุณต้องการ และโปรแกรมแสดงผลเริ่มต้นจะแสดงผลมุมมองแผนที่แทน
เริ่มต้น Maps SDK ก่อนเริ่มต้น Consumer SDK
ในคลาส Application
หรือสตาร์ทอัป Activity
ให้เรียกใช้
MapsInitializer.initialize()
รอผลลัพธ์ของคำขอโปรแกรมแสดงผลก่อนเริ่มต้น 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
แพตช์เพื่อสื่อสารกับเซิร์ฟเวอร์ ดูข้อมูลเพิ่มเติม
เกี่ยวกับการทำงานกับ SSL ใน Android ได้ที่ผู้ให้บริการ GMS ด้านความปลอดภัย
บทความนี้ยังมีตัวอย่างโค้ดสำหรับการแก้ไขผู้ให้บริการด้านความปลอดภัยด้วย
ขั้นตอนถัดไป
ตั้งค่าแผนที่
เนื้อหาของหน้าเว็บนี้ได้รับอนุญาตภายใต้ใบอนุญาตที่ต้องระบุที่มาของครีเอทีฟคอมมอนส์ 4.0 และตัวอย่างโค้ดได้รับอนุญาตภายใต้ใบอนุญาต Apache 2.0 เว้นแต่จะระบุไว้เป็นอย่างอื่น โปรดดูรายละเอียดที่นโยบายเว็บไซต์ Google Developers Java เป็นเครื่องหมายการค้าจดทะเบียนของ Oracle และ/หรือบริษัทในเครือ
อัปเดตล่าสุด 2025-08-31 UTC
[null,null,["อัปเดตล่าสุด 2025-08-31 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)"]]