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 KTX 及相关示例。
安装
要为 Maps SDK for Android 和 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 中,依次选择文件 -> 打开,前往您刚刚克隆或下载文件的文件夹并将其打开。
- 向演示版应用添加 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。
- 选择运行“app-ktx”。
后续内容
您可能还会对适用于 Google Maps Platform 的其他 Kotlin 扩展程序库感兴趣:
- 适用于 Maps SDK for Android 实用程序库的 KTX
- 适用于 Places SDK for Android 的 KTX
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2024-04-17。
[null,null,["最后更新时间 (UTC):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"]]