From 41e6322ccbcd7a22d3994f16dc73521e833c8fea Mon Sep 17 00:00:00 2001 From: Sean Barbeau Date: Wed, 6 May 2020 12:24:13 -0400 Subject: [PATCH] feat: Use coroutine and AMU-KTX .awaitMap() to init basic map demo (#188) --- ApiDemos/kotlin/app/build.gradle | 6 ++++ .../kotlindemos/BasicMapDemoActivity.kt | 30 +++++++++---------- 2 files changed, 21 insertions(+), 15 deletions(-) diff --git a/ApiDemos/kotlin/app/build.gradle b/ApiDemos/kotlin/app/build.gradle index 907c022d..95eecea9 100644 --- a/ApiDemos/kotlin/app/build.gradle +++ b/ApiDemos/kotlin/app/build.gradle @@ -36,6 +36,12 @@ dependencies { implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" implementation 'com.android.support.constraint:constraint-layout:1.1.3' + // Google Maps KTX extensions + implementation 'com.google.maps.android:maps-ktx:1.4.0' + + // Needed for coroutines + implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.2.0' + // Below is used to run the easypermissions library to manage location permissions // EasyPermissions is needed to help us request for permission to access location implementation 'pub.devrel:easypermissions:3.0.0' diff --git a/ApiDemos/kotlin/app/src/main/java/com/example/kotlindemos/BasicMapDemoActivity.kt b/ApiDemos/kotlin/app/src/main/java/com/example/kotlindemos/BasicMapDemoActivity.kt index 17296da4..0ff7159d 100644 --- a/ApiDemos/kotlin/app/src/main/java/com/example/kotlindemos/BasicMapDemoActivity.kt +++ b/ApiDemos/kotlin/app/src/main/java/com/example/kotlindemos/BasicMapDemoActivity.kt @@ -18,40 +18,40 @@ package com.example.kotlindemos import android.os.Bundle import androidx.appcompat.app.AppCompatActivity +import androidx.lifecycle.coroutineScope import com.google.android.gms.maps.CameraUpdateFactory import com.google.android.gms.maps.GoogleMap import com.google.android.gms.maps.OnMapReadyCallback import com.google.android.gms.maps.SupportMapFragment import com.google.android.gms.maps.model.LatLng import com.google.android.gms.maps.model.MarkerOptions +import com.google.maps.android.ktx.MapsExperimentalFeature +import com.google.maps.android.ktx.awaitMap /** * This shows how to create a simple activity with a map and a marker on the map. */ class BasicMapDemoActivity : - AppCompatActivity(), - OnMapReadyCallback { + AppCompatActivity() { val SYDNEY = LatLng(-33.862, 151.21) val ZOOM_LEVEL = 13f + @MapsExperimentalFeature override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_basic_map_demo) - val mapFragment : SupportMapFragment? = - supportFragmentManager.findFragmentById(R.id.map) as? SupportMapFragment - mapFragment?.getMapAsync(this) - } + val mapFragment = supportFragmentManager.findFragmentById(R.id.map) as? SupportMapFragment - /** - * This is where we can add markers or lines, add listeners or move the camera. In this case, - * we just move the camera to Sydney and add a marker in Sydney. - */ - override fun onMapReady(googleMap: GoogleMap?) { - googleMap ?: return - with(googleMap) { - moveCamera(CameraUpdateFactory.newLatLngZoom(SYDNEY, ZOOM_LEVEL)) - addMarker(MarkerOptions().position(SYDNEY)) + lifecycle.coroutineScope.launchWhenCreated { + check(mapFragment != null) + val googleMap = mapFragment.awaitMap() // Execution pauses here until we get the callback from the Maps SDK with a googleMap. + // This is where we can add markers or lines, add listeners or move the camera. + // In this case, we just move the camera to Sydney and add a marker in Sydney. + with(googleMap) { + moveCamera(CameraUpdateFactory.newLatLngZoom(SYDNEY, ZOOM_LEVEL)) + addMarker(MarkerOptions().position(SYDNEY)) + } } } }