지도 Android Kotlin 확장 프로그램(KTX)은 Android용 Maps SDK와 Android용 Maps SDK 유틸리티 라이브러리를 위한 Kotlin 확장 프로그램 모음입니다. 이 확장 프로그램에서는 Android용 Maps SDK 기반으로 개발할 때 간결하고 관용적인 Kotlin을 작성할 수 있는 Kotlin 언어 기능을 제공합니다. 지도 KTX는 오픈소스로 제공되며 GitHub에서 예시와 함께 제공됩니다.
설치
Android용 Maps SDK 및 Android용 Maps SDK 유틸리티 라이브러리(선택사항)용 KTX를 설치하려면 build.gradle
파일에 다음 종속 항목을 추가하세요.
dependencies {
// KTX for the Maps SDK for Android
implementation 'com.google.maps.android:maps-ktx:3.2.1'
// (Optional) KTX for the Maps SDK for Android Utility Library
implementation 'com.google.maps.android:maps-utils-ktx:3.2.1'
}
사용 예:
KTX 라이브러리를 사용하면 확장 프로그램 기능, 이름이 지정된 매개변수, 기본 인수, 비구조화 선언, 코루틴 등의 여러 Kotlin 언어 기능을 활용할 수 있습니다.
코루틴을 사용하여 GoogleMap 검색
다음과 같이 코루틴을 사용하여 GoogleMap
을 검색할 수 있습니다.
lifecycleScope.launchWhenCreated { 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.launchWhenCreated { googleMap.cameraMoveEvents().collect { print("Received camera move event") } }
지원되는 기능의 전체 목록은 참조 문서에서 확인할 수 있습니다.