Maps Android KTX
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
Maps Android Kotlin 擴充功能 (KTX) 是 Maps SDK for Android 和 Maps SDK for Android 公用程式庫的 Kotlin 擴充功能組合。這些擴充功能提供 Kotlin 語言功能,可讓您在開發 Maps SDK for Android 時,撰寫出簡潔和慣用的 Kotlin 程式語言。Maps KTX 為開放原始碼,您可以在 GitHub 中找到完整程式碼和相關範例。
安裝
若想安裝 Maps SDK for Android 適用的 KTX,以及 Maps SDK for Android 公用程式庫適用的 KTX (選用),請在 build.gradle
檔案中加入以下依附元件。
dependencies {
// KTX for the Maps SDK for Android library
implementation 'com.google.maps.android:maps-ktx:5.0.0'
}
使用範例
KTX 程式庫提供多項 Kotlin 語言功能,例如擴充功能函式、具名參數和預設引數、解構宣告,以及協同程式。
使用協同程式擷取 GoogleMap
您可使用協同程式擷取 GoogleMap
。
lifecycleScope.launch {
lifecycle.repeatOnLifecycle(Lifecycle.State.CREATED) {
val mapFragment: SupportMapFragment? =
supportFragmentManager.findFragmentById(R.id.map) as? SupportMapFragment
val googleMap: GoogleMap? = mapFragment?.awaitMap()
}
}
新增標記
您可以使用 DSL 式方法 addMarker()
來新增標記。
val sydney = LatLng(-33.852, 151.211)
val marker = googleMap.addMarker {
position(sydney)
title("Marker in Sydney")
}
收集攝影機事件
您可以透過 Kotlin 流程收集攝影機移動等事件。
lifecycleScope.launch {
lifecycle.repeatOnLifecycle(Lifecycle.State.CREATED) {
googleMap.cameraMoveEvents().collect {
print("Received camera move event")
}
}
}
如要查看支援功能的完整清單,請參閱參考說明文件。
使用範例應用程式
這個程式庫的 GitHub 存放區也包含試用版應用程式,可協助您瞭解如何在自有應用程式中使用 Maps KTX 程式庫。
如要使用試用版應用程式,請按照下列步驟操作:
- 在 GitHub 中,複製或下載 ZIP 檔案。
- 在 Android Studio 中,依序選擇「File」(檔案) 和「Open」(開啟),然後前往該目錄,開啟剛才複製或下載的資料夾。
- 在試用版應用程式中加入 API 金鑰。
- 取得 Maps SDK for Android 金鑰。
- 在根目錄中,建立名為
secrets.properties
的檔案。為保護您的 API 金鑰,這個檔案不得受到版本管控。
- 在
secrets.properties
中加入這一行
MAPS_API_KEY="YOUR_API_KEY"
其中 YOUR_API_KEY
是您在第一個步驟取得的 API 金鑰。您可以將 secrets.defaults.properties
視為例子。
- 在執行設定下,選取 app-ktx 模組。
- 選取「Run 'app-ktx'」(執行「app-ktx」)。
後續主題
我們也建議您查看 Google 地圖平台的其他 Kotlin 擴充功能程式庫:
- Map SDK for Android 公用程式庫專用的 KTX
- Places SDK for Android 專用的 KTX
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2024-04-17 (世界標準時間)。
[null,null,["上次更新時間:2024-04-17 (世界標準時間)。"],[[["\u003cp\u003eMaps Android Kotlin extensions (KTX) offer Kotlin features for concise and idiomatic development with the Maps SDK for Android.\u003c/p\u003e\n"],["\u003cp\u003eYou can add markers, retrieve GoogleMap using coroutines, and collect camera events using Kotlin Flows with Maps KTX.\u003c/p\u003e\n"],["\u003cp\u003eThe library is open-source and available on GitHub with a demo application showcasing its functionalities.\u003c/p\u003e\n"],["\u003cp\u003eMaps KTX can be easily installed by adding a dependency to your \u003ccode\u003ebuild.gradle.kts\u003c/code\u003e file.\u003c/p\u003e\n"],["\u003cp\u003eOther KTX libraries are available for the Maps SDK for Android Utility Library and Places SDK for Android.\u003c/p\u003e\n"]]],[],null,["Maps Android Kotlin extensions (KTX) are a collection of Kotlin extensions for the Maps SDK for\nAndroid and the Maps SDK for Android Utility Library. These extensions provide\nKotlin language features that enable you to write concise and idiomatic Kotlin\nwhen developing for the Maps SDK for Android. Maps KTX is open-sourced and\navailable on [GitHub](https://github.com/googlemaps/android-maps-ktx) along with\nexamples.\n\nInstallation\n\nTo install KTX for the Maps SDK for Android, and optionally for the Maps SDK for\nAndroid Utility Library, add the following dependencies to your `build.gradle.kts`\nfile. \n\n```carbon\ndependencies {\n\n // KTX for the Maps SDK for Android library\n implementation(\"com.google.maps.android:maps-ktx:5.2.0\")\n}\n```\n\nExample Usages\n\nWith the KTX library, you can take advantage of several Kotlin language\nfeatures such as extension functions, named parameters and default arguments,\ndestructuring declarations, and coroutines.\n\nRetrieving a GoogleMap using coroutines\n\nAccessing a [`GoogleMap`](/maps/documentation/android-sdk/reference/com/google/android/libraries/maps/GoogleMap) can be retrieved\nusing coroutines. \n\n```scalate-server-page\nlifecycleScope.launch {\n lifecycle.repeatOnLifecycle(Lifecycle.State.CREATED) {\n val mapFragment: SupportMapFragment? =\n supportFragmentManager.findFragmentById(R.id.map) as? SupportMapFragment\n val googleMap: GoogleMap? = mapFragment?.awaitMap()\n }\n}\n```\n\nAdding a marker\n\nAdding a marker can be done using the DSL-style method `addMarker()`. \n\n```text\nval sydney = LatLng(-33.852, 151.211)\nval marker = googleMap.addMarker {\n position(sydney)\n title(\"Marker in Sydney\")\n}\n```\n\nCollecting camera events\n\nEvents, such as camera moves, can be collected via [Kotlin Flow](https://developer.android.com/kotlin/flow). \n\n```povray\nlifecycleScope.launch {\n lifecycle.repeatOnLifecycle(Lifecycle.State.CREATED) {\n googleMap.cameraMoveEvents().collect {\n print(\"Received camera move event\")\n }\n }\n}\n```\n\nYou can see a full list of supported features by reading the\n[reference documentation](https://googlemaps.github.io/android-maps-ktx).\n\nTry the sample application\n\nThe GitHub repository for this library also contains a [demo application](https://github.com/googlemaps/android-maps-ktx/tree/main/app)\nthat shows how you can use the Maps KTX library in your own app.\n\nTo try the demo application, follow these steps:\n\n1. From [GitHub](https://github.com/googlemaps/android-maps-ktx), clone the or download the ZIP file.\n2. In Android Studio, choose **File -\\\u003e Open** and navigate to the directory and open the folder that you just cloned or downloaded.\n3. Add an API key to the demo app.\n 1. [Get a Maps SDK for Android key](/maps/documentation/android-sdk/get-api-key).\n 2. In the root directory, create a file called `secrets.properties`. This file should NOT be under version control to protect your API key.\n 3. Add this single line to `secrets.properties` \n\n ```\n MAPS_API_KEY=\"YOUR_API_KEY\"\n ```\n where `YOUR_API_KEY` is the actual API key you obtained in the first step. You can look at the [`secrets.defaults.properties`](https://github.com/googlemaps/android-maps-ktx/blob/main/secrets.defaults.properties) as an example.\n4. Under the run configuration, select the module **app-ktx**.\n5. Select **Run 'app-ktx'.**\n\nWhat's next\n\nYou may also be interested in other Kotlin extension libraries for Google Maps\nPlatform:\n\n- [KTX](/maps/documentation/android-sdk/utility/setup#ktx) for the Map SDK for Android Utility Library\n- [KTX](/maps/documentation/places/android-sdk/ktx#install) for the Places SDK for Android"]]