使用 Android XR、Geospatial API 和 Gemini 构建混合现实导游

阅读用时:7 分钟

在今年的 Google I/O 大会上,我们宣布了一项空间体验更新:Geospatial API 现已在 ARCore for Jetpack XR 中以预览版的形式提供。通过将 Google 的 Visual Positioning System (VPS) 引入 Android XR,Android XR 能够在支持的区域内以亚米级精度和精确的方向将数字内容锚定到现实世界中。*为了探索 Geospatial API 的潜力,我们的团队构建了一个演示:XR Geospatial Tour。

想象一下,您走进一座新城市,戴上一副有线 XR 眼镜(例如即将推出的 XREAL Project Aura),立即就会有一位知识渊博的当地导游带您四处游览。您无需低头查看 2D 地图,而是通过 3D 模型轻松找到路线,并借助智能语音了解眼前的历史地标。我们结合使用了 Geospatial API使用 Firebase AI Logic 的 Gemini APIGoogle 地图 Grounding 和 Jetpack XR SDK,打造了沉浸式免手动步行导览体验。

 

 

免责声明:视频和导览应用仅用于演示目的。部分操作步骤已精简。所展示的任何硬件可能尚处于开发阶段,最终产品细节可能会有所不同。

接下来,我们来详细了解一下实现细节,并展示如何将这些 API 结合在一起,打造出世界级的空间体验。

1. 使用 ARCore Geospatial API (VPS) 精确定位用户

将 GPS 的强大功能与 VPS 的精确性相结合,提升 XR 导航体验。VPS 提供的精确度和准确的方向信息可让 3D 航点与现实世界保持一致。

因此,Android XR 上的 Geospatial API 可以帮助您打造自定义体验。通过使用先进的计算机视觉技术,VPS 尝试提供比 GPS 更准确的 GeospatialPose(包括纬度、经度和航向)。

以下是我们通过将设备的屏幕方向映射到地理空间坐标来检索用户的地理空间姿态的方式:

// Retrieve the current geospatial pose from the ARCore session
val result = geospatial.createGeospatialPoseFromPose(arDevice.state.value.devicePose)
if (result is CreateGeospatialPoseFromPoseSuccess) {
    val pose = result.pose
    Log.d("VPS", "Accurate Location: ${pose.latitude}, ${pose.longitude}")
}

由于整个体验都依赖于这种准确性,因此我们会监控 horizontalAccuracy 和 orientationYawAccuracy,直到它们达到我们的阈值。如果用户在室内或在无法识别的区域,我们会提示他们“走到室外公共空间,然后环顾四周”。

2. 使用 Gemini API 和 Google 地图接地功能制定行程

获得位置信息后,我们使用 Firebase AI Logic 的 Gemini API 提示 Gemini 模型充当当地导游。我们将用户的坐标传递给模型,并要求模型输出包含附近徒步游的结构化 JSON 响应:

   val configForTools = ToolConfig(
      functionCallingConfig = null,
      retrievalConfig = retrievalConfig {
        latLng = FirebaseLatLng(pose.latitude, pose.longitude)
        languageCode = "en"
      }
    )

    val responseJsonSchema = Schema.obj(
      mapOf(
        "locationIntro" to Schema.string(),
        "tours" to Schema.array(
          Schema.obj(
            mapOf(
              "title" to Schema.string(),
              "description" to Schema.string(),
              "stops" to Schema.array(
                Schema.obj(
                  mapOf(
                    "name" to Schema.string(),
                    "detailedName" to Schema.string(),
                    "description" to Schema.string()
                  )
                )
              )
            )
          )
        )
      )
    )

    val model = Firebase.ai(backend = GenerativeBackend.googleAI()).generativeModel(
      modelName = "gemini-3.5-flash",
      tools = listOf(Tool.googleMaps()),
      generationConfig = generationConfig {
        responseMimeType = "application/json"
        responseSchema = responseJsonSchema
      }
    )

   val result = model.generateContent("The user is at latitude ${pose.latitude} and longitude ${pose.longitude}. Generate exactly 3 diverse tours near this location (e.g., historical, food, nature). All tour ideas should be walking distance only.")

大语言模型非常擅长生成丰富详尽的描述,但有时会产生精确的纬度/经度坐标幻觉。为了解决这个问题,我们使用 Google 地图接地来接地 AI。

3. 为你指路的语音:Gemini 2.5 TTS

为了让导游感觉真正身临其境,我们实现了动态旁白。

借助 gemini-2.5-flash-tts model,我们可以配置模型生成配置,以原生方式返回音频数据,而不仅仅是文本!您可以按以下步骤申请 ResponseModality.AUDIO

val ttsModel = Firebase.ai(backend = GenerativeBackend.googleAI())
    .generativeModel(
        modelName = "gemini-2.5-flash-tts",
        generationConfig = generationConfig {
            // Instruct the model to return Audio
            responseModalities = listOf(ResponseModality.AUDIO)
        }
    )

val response = ttsModel.generateContent("Say in a neutral but positive voice:\n$prompt")

// Extract the raw audio bytes from the response
val audioBytes = response.candidates.firstOrNull()?.content?.parts
    ?.filterIsInstance<InlineDataPart>()
    ?.firstOrNull { it.mimeType.contains("audio") }?.inlineData

4. 利用 Jetpack XR 以 3D 形式让世界跃然眼前

最后一块拼图是将这些数据呈现在用户的视野中。借助 Jetpack XR SDK,您可以直观地从 2D Android 界面过渡到空间计算。

我们使用 Jetpack Compose for XR 构建了空间组件。为了表示导览中的兴趣点,我们构建了一个名为 InfoSphere 的可组合项,其中包含一个漂浮在空间中的 3D 球体的 GltfModel,用户可以通过与之互动来显示信息。

借助 Jetpack XR SDK,我们可以使用 SpatialBox 和 SceneCoreEntity 将 3D 模型放置在 Compose 界面旁边。我们还使用 InteractableComponent 来响应用户点按。

通过将传统 Compose 界面表面的 AnimatedSpatialVisibility 与 SceneCoreEntity 3D 元素相结合,我们可以将数据无缝融入现实世界。

@Composable
fun InfoSphere(
    content: InfoBubbleContent,
    session: Session,
    sphereModel: GltfModel,
    isSelected: Boolean,
    onClick: () -> Unit
) {
    // SpatialBox lets us arrange 3D components and SpatialPanels together
    SpatialBox(
        SubspaceModifier
            .offset(x = 2.dp, y = 1.dp, z = (-3).dp) // Positioned in 3D space
    ) {
        // Smoothly animate the visibility of our 2D Compose UI Panel
        AnimatedSpatialVisibility(visible = isSelected) {
            SpatialPanel {
                InfoBubble(content) // Regular 2D Compose UI
            }
        }
        // Render our interactive 3D sphere
        SceneCoreEntity(
            factory = {
                GltfModelEntity.create(session, sphereModel).also { entity ->
                    // Make the 3D model respond to user taps
                    entity.addComponent(InteractableComponent.create(session) { inputEvent ->
                        if (inputEvent.action == InputEvent.Action.UP) {
                            onClick()
                        }
                    })
                }
            }
        )
    }
}

探索 Android XR 目前能实现的功能

在构建 XR Geospatial Tour 应用的过程中,我们发现 Android 开发者现在可以更轻松地打造世界级空间体验。Geospatial API 现已在 Android XR 上推出预览版,您的应用可以无缝了解周围的物理世界。通过将 Compose for XR 的 API 与 VPS 的高精度位置数据和 Gemini 的生成功能相结合,我们可以打造出既能了解用户所在位置又能了解用户正在观看的内容的体验。

为了帮助您亲身体验 Android XR,我们很高兴宣布启动 Android XR 开发者催化剂计划,该计划包含 XREAL Project Aura。从今天起,您可以申请在未来几个月内获得 XREAL Project Aura 开发者套件或带屏眼镜开发者套件的访问权限!

*免责声明:适用于部分设备。需要连接到互联网。适用于兼容的应用和平台。实际情况可能会有所不同。

继续阅读