קל לארגן דפים בעזרת אוספים
אפשר לשמור ולסווג תוכן על סמך ההעדפות שלך.
Maps Android Kotlin extensions (KTX) הם אוסף של תוספים ל-Kotlin ל-SDK של מפות ל-Android ולספריית כלי העזר של SDK של מפות ל-Android. התוספים האלה מספקים תכונות של שפת Kotlin שמאפשרות לכתוב קוד Kotlin תמציתי ואידיומטי כשמפתחים באמצעות Maps SDK ל-Android. Maps KTX הוא קוד פתוח וזמין ב-GitHub, יחד עם דוגמאות.
התקנה
כדי להתקין את KTX עבור Maps SDK ל-Android, ואופציונלית עבור Maps SDK ל-Android Utility Library, מוסיפים את יחסי התלות הבאים לקובץ build.gradle.kts.
dependencies{// KTX for the Maps SDK for Android libraryimplementation("com.google.maps.android:maps-ktx:5.2.0")}
דוגמאות לשימוש
ספריית KTX מאפשרת לכם ליהנות מכמה תכונות של שפת Kotlin, כמו פונקציות הרחבה, פרמטרים בעלי שם וארגומנטים שמוגדרים כברירת מחדל, הצהרות על פירוק וקורוטינות.
[[["התוכן קל להבנה","easyToUnderstand","thumb-up"],["התוכן עזר לי לפתור בעיה","solvedMyProblem","thumb-up"],["סיבה אחרת","otherUp","thumb-up"]],[["חסרים לי מידע או פרטים","missingTheInformationINeed","thumb-down"],["התוכן מורכב מדי או עם יותר מדי שלבים","tooComplicatedTooManySteps","thumb-down"],["התוכן לא עדכני","outOfDate","thumb-down"],["בעיה בתרגום","translationIssue","thumb-down"],["בעיה בדוגמאות/בקוד","samplesCodeIssue","thumb-down"],["סיבה אחרת","otherDown","thumb-down"]],["עדכון אחרון: 2025-08-31 (שעון 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"]]