chore: Update basic map demo (#193)

Based on additional comments in https://github.com/googlemaps/android-samples/pull/188:
* Use KTX for addMarker()
* Allow optional for GoogleMap
This commit is contained in:
Sean Barbeau 2020-05-07 12:23:54 -04:00 committed by GitHub
parent 5e6540cc6b
commit 581ae289a6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -20,12 +20,10 @@ 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.addMarker
import com.google.maps.android.ktx.awaitMap
/**
@ -44,13 +42,14 @@ class BasicMapDemoActivity :
val mapFragment = supportFragmentManager.findFragmentById(R.id.map) as? SupportMapFragment
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.
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))
this?.moveCamera(CameraUpdateFactory.newLatLngZoom(SYDNEY, ZOOM_LEVEL))
this?.addMarker {
position(SYDNEY)
}
}
}
}