chore: Add KTX snippets. (#767)

* chore: Add KTX snippets.

Change-Id: I7b1ce2aaefa04b65682f29586f65bb042599ad18

* Import Maps explicitly.

Change-Id: I7148dbfc358fe3b30d5b99b2e252a03967a8d67f
This commit is contained in:
Chris Arriola 2021-12-02 16:58:47 -06:00 committed by GitHub
parent f5010678a3
commit f6b2da8bcb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 47 additions and 0 deletions

View File

@ -55,7 +55,9 @@ dependencies {
implementation 'androidx.navigation:navigation-fragment:2.3.5'
implementation 'androidx.navigation:navigation-ui:2.3.5'
implementation 'com.android.volley:volley:1.2.1'
implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.4.0'
gmsImplementation 'com.google.maps.android:maps-ktx:3.2.1'
gmsImplementation 'com.google.android.gms:play-services-maps:18.0.0'
gmsImplementation 'com.google.maps.android:android-maps-utils:2.3.0'
v3Implementation 'com.google.android.libraries.maps:maps:3.1.0-beta'

View File

@ -0,0 +1,45 @@
package com.google.maps.example.kotlin
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.lifecycleScope
import com.google.android.gms.maps.GoogleMap
import com.google.android.gms.maps.SupportMapFragment
import com.google.android.gms.maps.model.LatLng
import com.google.maps.android.ktx.addMarker
import com.google.maps.android.ktx.awaitMap
import com.google.maps.android.ktx.cameraMoveEvents
import com.google.maps.example.R
import kotlinx.coroutines.flow.collect
internal class KTX : AppCompatActivity() {
private lateinit var googleMap: GoogleMap
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
// [START maps_android_ktx_obtain_map]
lifecycleScope.launchWhenCreated {
val mapFragment: SupportMapFragment? =
supportFragmentManager.findFragmentById(R.id.map) as? SupportMapFragment
val googleMap: GoogleMap? = mapFragment?.awaitMap()
}
// [END maps_android_ktx_obtain_map]
// [START maps_android_ktx_add_marker]
val sydney = LatLng(-33.852, 151.211)
val marker = googleMap.addMarker {
position(sydney)
title("Marker in Sydney")
}
// [END maps_android_ktx_add_marker]
// [START maps_android_ktx_camera_events]
lifecycleScope.launchWhenCreated {
googleMap.cameraMoveEvents().collect {
print("Received camera move event")
}
}
// [END maps_android_ktx_camera_events]
}
}