การเริ่มต้นใช้งาน Cloud Anchor อย่างรวดเร็วสำหรับ Android
จัดทุกอย่างให้เป็นระเบียบอยู่เสมอด้วยคอลเล็กชัน
บันทึกและจัดหมวดหมู่เนื้อหาตามค่ากำหนดของคุณ
ลองโฮสต์และแก้ไข Cloud Anchor ในแอปตัวอย่าง cloud_anchor_java
และ persistent_cloud_anchor_java
คุณควรใช้แอปตัวอย่างใด
Cloud Anchor เป็นAnchor ที่โฮสต์ในปลายทางระบบคลาวด์ ARCore API API นี้ช่วยให้ผู้ใช้แชร์ประสบการณ์ในแอปเดียวกันได้ Cloud Anchor แบบถาวรคือ Cloud Anchor ที่สามารถโฮสต์ได้นานกว่า 24 ชั่วโมง
persistent_cloud_anchor_java
เป็นการปรับใช้กรณีการใช้งานโปรเจ็กต์แบบทำงานร่วมกัน ใช้การให้สิทธิ์แบบไม่ใช้คีย์เพื่อให้สิทธิ์การเรียก ARCore API เนื่องจากไม่ได้ใช้แบ็กเอนด์ของ Firebase เพื่อแชร์รหัส Cloud Anchor ระหว่างอุปกรณ์ จึงสามารถเริ่มต้นใช้งานได้ง่ายขึ้น
cloud_anchor_java
เป็นการใช้งานกรณีการใช้งานแบบหลายผู้เล่น และใช้คีย์ API เพื่อให้สิทธิ์การเรียก ARCore API
ข้อกำหนดเบื้องต้น
ข้อกำหนด
ฮาร์ดแวร์
ซอฟต์แวร์
- Android Studio เวอร์ชัน 3.0 ขึ้นไปที่ใช้แพลตฟอร์ม Android SDK เวอร์ชัน 7.0 (API ระดับ 24) ขึ้นไป
- ARCore SDK สำหรับ Android ซึ่งคุณดูได้ 2 วิธีดังนี้
- ดาวน์โหลดจาก GitHub และดึงข้อมูลลงในเครื่องของคุณ
- โคลนที่เก็บด้วยคำสั่งต่อไปนี้
git clone https://github.com/google-ar/arcore-android-sdk.git
เปิดแอปตัวอย่างใน Android Studio
ARCore SDK มีแอปตัวอย่าง cloud_anchor_java
และ persistent_cloud_anchor_java
ตัวอย่างเพื่อสาธิตฟังก์ชันการทำงานของ Cloud Anchor ทำตามขั้นตอนต่อไปนี้เพื่อเปิดแอปใน Android Studio
Cloud Anchor แบบถาวร
ใน Android Studio ให้คลิกเปิด
ไปยังตำแหน่งที่เก็บไดเรกทอรี arcore-android-sdk ในเครื่อง อย่าเปิดโฟลเดอร์ SDK ทั้งโฟลเดอร์ ให้ไปที่ตัวอย่าง > แทน persistent_cloud_anchor_java แล้วคลิกเปิด
ตัวยึดระบบคลาวด์
ใน Android Studio ให้คลิกเปิด
ไปยังตำแหน่งที่เก็บไดเรกทอรี arcore-android-sdk ในเครื่อง อย่าเปิดโฟลเดอร์ SDK ทั้งโฟลเดอร์ ให้ไปที่ตัวอย่าง > แทน cloud_anchor_java แล้วคลิกเปิด
ตั้งค่าการแชร์รหัส Cloud Anchor
รหัส Cloud Anchor คือสตริงที่ระบุ Cloud Anchor ที่โฮสต์ไว้ โดยจะใช้เพื่อแก้ไขหรือแสดงผลวัตถุ 3 มิติที่ติดอยู่กับ Anchor ที่โฮสต์ไว้
Cloud Anchor แบบถาวร
การแชร์รหัส Cloud Anchor จะได้รับการเก็บรักษาไว้ภายในแอป คุณไม่จำเป็นต้องดำเนินการใดๆ ที่นี่
ตัวยึดระบบคลาวด์
แอปตัวอย่าง cloud_anchor_java
ใช้ฐานข้อมูลแบบเรียลไทม์ของ Firebase เพื่อแชร์รหัส Cloud Anchor ระหว่างอุปกรณ์ คุณใช้โซลูชันอื่นในแอปของตัวเองได้
- เพิ่ม Firebase ลงในแอปด้วยตนเอง ชื่อแพ็กเกจ
cloud_anchor_java
คือ com.google.ar.core.examples.java.cloudanchor
ซึ่งดูได้ใน main/AndroidManifest.xml
- ดาวน์โหลดไฟล์
google-services.json
ที่คุณสร้างขึ้นเมื่อเพิ่ม Firebase ลงในแอป
- สร้าง Realtime Database ด้วย Firebase
- ใน Android Studio ให้เพิ่มไฟล์
google-services.json
ในไดเรกทอรี app
ของโปรเจ็กต์
ให้สิทธิ์การเรียก ARCore API
ให้สิทธิ์การเรียกไปยัง ARCore API เพื่อโฮสต์และแก้ปัญหา Cloud Anchor สำหรับ
แอป ทำตามขั้นตอนสำหรับใช้ ARCore API ใน Google
Cloud และใช้ Keyless
การให้สิทธิ์สำหรับ Persistent Cloud Anchor หรือการให้สิทธิ์คีย์ API สำหรับ
Cloud Anchor
สร้างและเรียกใช้แอปตัวอย่าง
Cloud Anchor แบบถาวร
เรียกใช้แอป
- ตรวจสอบว่าอุปกรณ์ได้เปิดใช้ตัวเลือกสำหรับนักพัฒนาแอปและการแก้ไขข้อบกพร่อง USB แล้ว
- เชื่อมต่ออุปกรณ์ผ่าน USB กับเครื่องพัฒนา
- ใน Android Studio ให้เลือกอุปกรณ์ของคุณเป็นเป้าหมายการทำให้ใช้งานได้ แล้วคลิกเรียกใช้
persistent_cloud_anchor_java
ควรเปิดขึ้นมาในอุปกรณ์เพื่อเตือนให้ ARCore ตรวจหาเครื่องบินที่อยู่หน้ากล้องของอุปกรณ์
หากให้สิทธิ์ Google Cloud ไม่สำเร็จ โปรดดูขั้นตอนการแก้ปัญหา
วาง Anchor
- เมื่อแอปเริ่มตรวจพบระนาบ ให้แตะหน้าจอเพื่อวางจุดยึดบนระนาบที่ตรวจพบ
- แตะปุ่มโฮสต์เพื่อโฮสต์ Anchor ที่วางอยู่ การดำเนินการนี้จะส่งคำขอโฮสต์ไปยัง ARCore API ที่มีข้อมูลที่แสดงตำแหน่งของ Anchor ที่เกี่ยวข้องกับฟีเจอร์ภาพที่อยู่ใกล้กับแท็ก
คำขอของโฮสต์ที่ดำเนินการสำเร็จจะสร้าง Anchor ที่ตำแหน่งที่วางและกำหนดรหัส Cloud Anchor ให้ หากคำขอของโฮสต์สำเร็จ แอปควรแสดงรหัสห้อง คุณสามารถใช้รหัสนี้เพื่อเข้าถึง Anchor ที่โฮสต์ไว้ก่อนหน้านี้สําหรับห้องนี้ในอุปกรณ์ใดก็ได้
แก้ไขแท็ก Anchor
- แตะแก้ปัญหา แล้วป้อนรหัสห้องที่ส่งกลับมาก่อนหน้านี้เพื่อเข้าถึง Anchor ที่โฮสต์ในห้องนี้ การดำเนินการนี้จะส่งคำขอแก้ไขไปยัง ARCore API ซึ่งจะส่งกลับรหัส Anchor ทั้งหมดที่โฮสต์ในห้องอยู่ในปัจจุบัน
persistent_cloud_anchor_java
จะใช้รหัสเหล่านี้เพื่อแสดงออบเจ็กต์ 3 มิติที่แนบอยู่กับ Anchor ที่โฮสต์ไว้
ตัวยึดระบบคลาวด์
เรียกใช้แอป
- ตรวจสอบว่าอุปกรณ์ของคุณเปิดใช้ตัวเลือกสำหรับนักพัฒนาแอปและการแก้ไขข้อบกพร่อง USB แล้ว
- เชื่อมต่ออุปกรณ์ผ่าน USB กับเครื่องพัฒนา
- ใน Android Studio ให้เลือกอุปกรณ์ของคุณเป็นเป้าหมายการทำให้ใช้งานได้ แล้วคลิกเรียกใช้
cloud_anchor_java
ควรเปิดขึ้นมาในอุปกรณ์เพื่อเตือนให้ ARCore ตรวจหาเครื่องบินที่อยู่หน้ากล้องของอุปกรณ์
ข้อผิดพลาด: com.google.firebase.database.DatabaseException: Failed to get FirebaseDatabase instance: Specify DatabaseURL within FirebaseApp or from your getInstance
หากพบข้อผิดพลาดนี้ ให้ตรวจสอบว่ามีพร็อพเพอร์ตี้ firebase_url
อยู่ใน google-services.json
คุณจะรับค่าที่ถูกต้องสำหรับพร็อพเพอร์ตี้นี้ได้โดยการตรวจสอบว่าได้สร้าง Realtime Database และดาวน์โหลด google-services.json
แล้ว
วาง Anchor
- เมื่อแอปเริ่มตรวจพบระนาบ ให้แตะหน้าจอเพื่อวางจุดยึดบนระนาบที่ตรวจพบ
- แตะปุ่มโฮสต์เพื่อโฮสต์ Anchor ที่วางไว้ การดำเนินการนี้จะส่งคำขอโฮสต์ไปยัง ARCore API ที่มีข้อมูลที่แสดงตำแหน่งของ Anchor ที่เกี่ยวข้องกับฟีเจอร์ภาพที่อยู่ใกล้กับแท็ก
คำขอของโฮสต์ที่ดำเนินการสำเร็จจะสร้าง Anchor ที่ตำแหน่งที่วางและกำหนดรหัส Cloud Anchor ให้ หากคำขอของโฮสต์สำเร็จ แอปควรแสดงรหัสห้อง คุณสามารถใช้รหัสนี้เพื่อเข้าถึง Anchor ที่โฮสต์ไว้ก่อนหน้านี้สําหรับห้องนี้ในอุปกรณ์ใดก็ได้
แก้ไขแท็ก Anchor
- แตะแก้ปัญหา แล้วป้อนรหัสห้องที่ส่งกลับมาก่อนหน้านี้เพื่อเข้าถึง Anchor ที่โฮสต์ในห้องนี้ การดำเนินการนี้จะส่งคำขอแก้ไขไปยัง ARCore API ซึ่งจะส่งกลับรหัส Anchor ทั้งหมดที่โฮสต์ในห้องอยู่ในปัจจุบัน
cloud_anchor_java
จะใช้รหัสเหล่านี้เพื่อแสดงออบเจ็กต์ 3 มิติที่แนบอยู่กับ Anchor ที่โฮสต์ไว้
สิ่งที่จะเกิดขึ้นหลังจากนี้
เนื้อหาของหน้าเว็บนี้ได้รับอนุญาตภายใต้ใบอนุญาตที่ต้องระบุที่มาของครีเอทีฟคอมมอนส์ 4.0 และตัวอย่างโค้ดได้รับอนุญาตภายใต้ใบอนุญาต Apache 2.0 เว้นแต่จะระบุไว้เป็นอย่างอื่น โปรดดูรายละเอียดที่นโยบายเว็บไซต์ Google Developers Java เป็นเครื่องหมายการค้าจดทะเบียนของ Oracle และ/หรือบริษัทในเครือ
อัปเดตล่าสุด 2025-07-26 UTC
[null,null,["อัปเดตล่าสุด 2025-07-26 UTC"],[[["\u003cp\u003eExplore Cloud Anchors functionality using the \u003ccode\u003ecloud_anchor_java\u003c/code\u003e and \u003ccode\u003epersistent_cloud_anchor_java\u003c/code\u003e sample apps within the ARCore SDK.\u003c/p\u003e\n"],["\u003cp\u003ePersistent Cloud Anchors enable collaborative projects and persist for over 24 hours, while standard Cloud Anchors support multi-player use cases.\u003c/p\u003e\n"],["\u003cp\u003eBefore running the apps, ensure you have the necessary hardware and software, including an ARCore-supported device and Android Studio.\u003c/p\u003e\n"],["\u003cp\u003eAuthorize ARCore API calls using keyless authorization for Persistent Cloud Anchors and API key authorization for Cloud Anchors.\u003c/p\u003e\n"],["\u003cp\u003eHost and resolve anchors within the sample apps to experience Cloud Anchor functionality, placing and rendering 3D objects in shared AR experiences.\u003c/p\u003e\n"]]],["The content guides users to experiment with Cloud Anchors using two sample apps: `cloud_anchor_java` and `persistent_cloud_anchor_java`. These apps demonstrate hosting and resolving anchors via the ARCore API. Key actions include: installing prerequisites, opening the sample app in Android Studio, setting up Cloud Anchor ID sharing (using either keyless authorization or API key authorization), running the app, placing and hosting an anchor, and resolving an anchor using a room code. `Persistent_cloud_anchor_java` uses keyless authorization and local ID sharing. `cloud_anchor_java` uses API key authorization and Firebase for ID sharing.\n"],null,["# Cloud Anchors quickstart for Android\n\nTry out hosting and resolving [Cloud Anchors](/ar/develop/java/cloud-anchors/introduction) in the `cloud_anchor_java` and `persistent_cloud_anchor_java` sample apps.\n\nWhich sample app should you use?\n--------------------------------\n\nCloud Anchors are [anchors](/ar/develop/java/anchors) that are hosted on the [**ARCore API**](https://console.cloud.google.com/apis/library/arcore) cloud endpoint. This API enables users to share experiences in the same app. **Persistent Cloud Anchors** are Cloud Anchors that can be hosted for more than 24 hours.\n\n- `persistent_cloud_anchor_java` is an implementation of a collaborative project use case. It uses keyless authorization to authorize ARCore API calls. Because it doesn't use a Firebase backend to share Cloud Anchor IDs between devices, it is much easier to get up and running.\n- `cloud_anchor_java` is an implementation of a multi-player use case. It uses an API key to authorize ARCore API calls.\n\nPrerequisites\n-------------\n\n- Understand how [anchors](/ar/develop/java/anchors) and [Cloud Anchors](/ar/develop/java/cloud-anchors/introduction) work.\n- [Install and configure](/ar/develop/java/quickstart) the ARCore SDK for Android.\n\nRequirements\n------------\n\n### Hardware\n\n- An [ARCore supported device](/ar/devices) with the latest version of [Google Play Services for AR](https://play.google.com/store/apps/details?id=com.google.ar.core) installed\n- A USB cable to connect the device to your development machine\n\n### Software\n\n- [Android Studio](https://developer.android.com/studio) version 3.0 or later with Android SDK Platform version 7.0 (API level 24) or higher\n- The ARCore SDK for Android, which you can get in one of two ways:\n - Download it from [GitHub](https://github.com/google-ar/arcore-android-sdk) and extract it on your machine\n - Clone the repository with the following command: \n\n ```\n git clone https://github.com/google-ar/arcore-android-sdk.git\n ```\n\nOpen the sample app in Android Studio\n-------------------------------------\n\nThe ARCore SDK provides the `cloud_anchor_java` and `persistent_cloud_anchor_java` sample apps to demonstrate Cloud Anchors functionality. Follow these steps to open the apps in Android Studio. \n\n### Persistent Cloud Anchors\n\n1. In Android Studio, click **Open**.\n\n2. Navigate to the place where the **arcore-android-sdk** directory is stored on your machine. Do not open the entire SDK folder. Instead, go to **samples** \\\u003e **persistent_cloud_anchor_java** and click **Open**.\n\n### Cloud Anchors\n\n1. In Android Studio, click **Open**.\n\n2. Navigate to the place where the **arcore-android-sdk** directory is stored on your machine. Do not open the entire SDK folder. Instead, go to **samples** \\\u003e **cloud_anchor_java** and click **Open**.\n\nSet up Cloud Anchor ID sharing\n------------------------------\n\n**Cloud Anchor IDs** are strings that identify hosted Cloud Anchors. They are used to resolve, or render the 3D objects attached to, the hosted anchors. \n\n### Persistent Cloud Anchors\n\nCloud Anchor ID sharing is maintained locally in the app. You won't need to do anything here.\n\n### Cloud Anchors\n\nThe `cloud_anchor_java` sample app uses Firebase's Realtime Databases to share Cloud Anchor IDs between devices. You can use a different solution in your own apps.\n\n1. [Manually add Firebase to your app.](https://firebase.google.com/docs/android/setup#manually_add_firebase) The `cloud_anchor_java` package name is `com.google.ar.core.examples.java.cloudanchor`. You can find it in `main/AndroidManifest.xml`.\n2. Download the `google-services.json` file that you generated when you added Firebase to your app.\n3. [Create a Realtime Database with Firebase.](https://firebase.google.com/docs/database/android/start#create_a_database)\n4. In Android Studio, add the `google-services.json` file to your project's `app` directory.\n\nAuthorize ARCore API calls\n--------------------------\n\nAuthorize calls to the ARCore API to host and resolve Cloud Anchors for your\napp. Follow steps for on [Use the ARCore API on Google\nCloud](/ar/develop/authorization?platform=android) and use **Keyless\nauthorization** for Persistent Cloud Anchors or **API Key authorization** for\nCloud Anchors.\n\nBuild and run the sample app\n----------------------------\n\n### Persistent Cloud Anchors\n\n### Run the app\n\n1. Make sure that your device has enabled [developer options and USB debugging](https://developer.android.com/studio/debug/dev-options).\n2. Connect your device via USB to your development machine.\n3. In Android Studio, select your device as the deployment target and click **Run**.\n\n`persistent_cloud_anchor_java` should launch on your device, prompting ARCore to detect the planes in front of the device's camera.\n\nIf Google Cloud authorization fails, see [Troubleshooting steps](/ar/develop/authorization/troubleshooting?platform=android).\n\n### Place an anchor\n\n1. Once the app begins to detect planes, tap your screen to place an anchor on one of the detected planes.\n2. Tap the **HOST** button to host the placed anchor. This sends a host request to the ARCore API, which includes data representing the anchor's position relative to the visual features near it.\n\nA successful host request establishes an anchor at the placed location and assigns it a Cloud Anchor ID. If the host request is successful, the app should display a room code. You can use this code to access previously hosted anchors for this room on any device.\n\n### Resolve an anchor\n\n1. Tap **RESOLVE** and enter a previously returned room code to access the anchors hosted in this room. This sends a resolve request to the ARCore API, which returns the IDs of all anchors currently hosted in the room. `persistent_cloud_anchor_java` will use these IDs to render 3D objects attached to the hosted anchors.\n\n### Cloud Anchors\n\n### Run the app\n\n1. Make sure that your device has enabled [developer options and USB debugging](https://developer.android.com/studio/debug/dev-options).\n2. Connect your device via USB to your development machine.\n3. In Android Studio, select your device as the deployment target and click **Run**.\n\n`cloud_anchor_java` should launch on your device, prompting ARCore to detect the planes in front of the device's camera. \n\n#### Error: `com.google.firebase.database.DatabaseException: Failed to get FirebaseDatabase instance: Specify DatabaseURL within FirebaseApp or from your getInstance`\n\nIf you encounter this error, ensure that the `firebase_url` property is present in `google-services.json`. You can obtain the correct value for this property by ensuring that the Realtime Database has been created and downloading `google-services.json`.\n\n*** ** * ** ***\n\n### Place an anchor\n\n1. Once the app begins to detect planes, tap your screen to place an anchor on one of the detected planes.\n2. Tap the **HOST** button to host the placed anchor. This sends a host request to the ARCore API, which includes data representing the anchor's position relative to the visual features near it.\n\nA successful host request establishes an anchor at the placed location and assigns it a Cloud Anchors ID. If the host request is successful, the app should display a room code. You can use this code to access previously hosted anchors for this room on any device.\n\n### Resolve an anchor\n\n1. Tap **RESOLVE** and enter a previously returned room code to access the anchors hosted in this room. This sends a resolve request to the ARCore API, which returns the IDs of all anchors currently hosted in the room. `cloud_anchor_java` will use these IDs to render 3D objects attached to the hosted anchors.\n\nWhat's next\n-----------\n\n- Create a Cloud Anchors app with the [ARCore Cloud Anchors with persistent Cloud Anchors codelab](https://codelabs.developers.google.com/codelabs/arcore-cloud-anchors#0).\n- Learn how to incorporate Cloud Anchors in your app with the [Cloud Anchors developer guide](/ar/develop/java/cloud-anchors/developer-guide).\n- Manage Cloud Anchors outside of your ARCore app using the [Cloud Anchors Management API](/ar/develop/java/cloud-anchors/management-api)."]]