本頁面將逐步說明範例,瞭解如何使用 Maps 3D SDK for Android,在 Android 應用程式中加入基本 3D 地圖。本頁的說明假設您已完成「設定」頁面的步驟,並具備下列條件:
- 已啟用 Maps 3D SDK for Android 的 Google Cloud 專案
- 設定為搭配 Maps 3D SDK for Android 使用的 API 金鑰
- 已設定為搭配 Maps 3D SDK for Android 使用的 Android Studio 專案
如要進一步瞭解這些必要條件,請參閱「設定」一節。
第 1 部分:更新版面配置檔案 (activity_main.xml
),加入 Map3DView
元件
Map3DView
元件是應用程式中用於算繪 3D 地圖的檢視區塊。下列步驟會新增元件,並設定地圖的初始狀態,包括攝影機位置和相關屬性:
開啟主要活動的版面配置檔案,通常位於
app/src/main/res/layout/activity_main.xml
。在根
ConstraintLayout
(或根版面配置元素) 中,新增map3d
XML 命名空間:xmlns:map3d="http://schemas.android.com/apk/res-auto"
刪除顯示「Hello World!」的預設
<TextView>
。在版面配置中加入
Map3DView
元件。您可以自訂攝影機位置和其他屬性:<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:map3d="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/main" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <com.google.android.gms.maps3d.Map3DView android:id="@+id/map3dView" android:layout_width="match_parent" android:layout_height="match_parent" map3d:mode="hybrid" map3d:centerLat="38.544012" map3d:centerLng="-107.670428" map3d:centerAlt="2427.6" map3d:heading="310" map3d:tilt="63" map3d:range="8266" map3d:roll="0" map3d:minAltitude="0" map3d:maxAltitude="1000000" map3d:minHeading="0" map3d:maxHeading="360" map3d:minTilt="0" map3d:maxTilt="90" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> </androidx.constraintlayout.widget.ConstraintLayout>
第 2 部分:更新 MainActivity.kt
下列步驟會初始化在第 1 部分中新增至 activity_main.xml
檔案的 Map3DView
元件,並管理元件生命週期事件:
開啟
MainActivity.kt
檔案,通常位於app/src/main/java/com/example/yourpackagename/MainActivity.kt
。為 Maps 3D SDK for Android 新增必要匯入項目:
import com.google.android.gms.maps3d.GoogleMap3D import com.google.android.gms.maps3d.Map3DView import com.google.android.gms.maps3d.OnMap3DViewReadyCallback
修改
MainActivity
類別,實作OnMap3DViewReadyCallback
:class MainActivity : AppCompatActivity(), OnMap3DViewReadyCallback {
宣告
Map3DView
和GoogleMap3D
的變數:private lateinit var map3DView: Map3DView private var googleMap3D: GoogleMap3D? = null
在
onCreate
方法中,於setContentView(...)
和ViewCompat.setOnApplyWindowInsetsListener
區塊之後,初始化map3DView
、呼叫其onCreate
生命週期方法,並非同步要求地圖:override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) enableEdgeToEdge() setContentView(R.layout.activity_main) ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets -> val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars()) v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom) insets } map3DView = findViewById(R.id.map3dView) map3DView.onCreate(savedInstanceState) map3DView.getMap3DViewAsync(this) }
覆寫
onMap3DViewReady
方法。地圖準備就緒時就會觸發這個回呼:override fun onMap3DViewReady(googleMap3D: GoogleMap3D) { // Interact with the googleMap3D object here this.googleMap3D = googleMap3D // You can now make calls to the googleMap3D object, e.g., // googleMap3D.cameraController.flyTo(camera { ... }) }
將 Activity 的生命週期事件轉送至
Map3DView
,方法是在MainActivity
中加入下列覆寫項目:override fun onStart() { super.onStart() map3DView.onStart() } override fun onResume() { super.onResume() map3DView.onResume() } override fun onPause() { map3DView.onPause() super.onPause() } override fun onStop() { map3DView.onStop() super.onStop() } override fun onDestroy() { map3DView.onDestroy() super.onDestroy() } override fun onSaveInstanceState(outState: Bundle) { super.onSaveInstanceState(outState) map3DView.onSaveInstanceState(outState) } override fun onLowMemory() { super.onLowMemory() map3DView.onLowMemory() }
第 3 部分:同步處理 Gradle 並執行
更新應用程式的版面配置和活動後,您就可以建構並執行應用程式,查看 3D 地圖檢視畫面。
如要將專案與 Gradle 同步處理,請依序選取「File」>「Sync Project with Gradle Files」。
如要在模擬器或實體裝置上建構及執行應用程式,請選取「Run」>「Run」。
如果一切設定正確,應用程式中應該會顯示 3D 地圖,並以您在 activity_main.xml
中指定的座標為中心。
後續步驟
您已在應用程式中加入基本 3D 地圖,現在可以探索 Maps 3D SDK for Android 的進階功能,例如攝影機路徑動畫、3D 標記或多邊形。