Maps Android KTX
コレクションでコンテンツを整理
必要に応じて、コンテンツの保存と分類を行います。
Maps Android Kotlin 拡張機能(KTX)には、Maps SDK for Android と Maps SDK for Android ユーティリティ ライブラリ用の Kotlin 拡張機能がまとめられています。これらの拡張機能を使用すると、Maps SDK for Android 向けの開発を行う際に、簡潔でわかりやすい Kotlin を作成するための Kotlin 言語機能を利用できます。Maps KTX はオープンソースで、例と合わせて GitHub で入手できます。
インストール
Maps SDK for Android 用と Maps SDK for Android ユーティリティ ライブラリ用(省略可)の KTX をインストールするには、build.gradle.kts
ファイルに次の依存関係を追加します。
dependencies {
// KTX for the Maps SDK for Android library
implementation("com.google.maps.android:maps-ktx:5.1.1")
}
使用例
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 Flow で収集できます。
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 キーを守るため、このファイルはバージョン管理の対象にしてはいけません。
- 次の 1 行を
secrets.properties
に追加します。
MAPS_API_KEY="YOUR_API_KEY"
この行の YOUR_API_KEY
は、最初の手順で取得した実際の API キーです。secrets.defaults.properties
は一例としてご覧ください。
- [Run configuration] で、モジュール app-ktx を選択します。
- [Run 'app-ktx'] を選択します。
次のステップ
Google Maps Platform の次の Kotlin 拡張機能についても、利用をご検討ください。
- Map SDK for Android ユーティリティ ライブラリ用 KTX
- Places SDK for Android 用 KTX
特に記載のない限り、このページのコンテンツはクリエイティブ・コモンズの表示 4.0 ライセンスにより使用許諾されます。コードサンプルは Apache 2.0 ライセンスにより使用許諾されます。詳しくは、Google Developers サイトのポリシーをご覧ください。Java は Oracle および関連会社の登録商標です。
最終更新日 2024-12-03 UTC。
[null,null,["最終更新日 2024-12-03 UTC。"],[[["\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"]]