@@ -18,39 +18,40 @@ package com.example.kotlindemos
18
18
19
19
import android.os.Bundle
20
20
import androidx.appcompat.app.AppCompatActivity
21
- import androidx.lifecycle.coroutineScope
22
21
import com.google.android.gms.maps.CameraUpdateFactory
22
+ import com.google.android.gms.maps.GoogleMap
23
+ import com.google.android.gms.maps.OnMapReadyCallback
23
24
import com.google.android.gms.maps.SupportMapFragment
24
25
import com.google.android.gms.maps.model.LatLng
25
- import com.google.maps.android.ktx.MapsExperimentalFeature
26
- import com.google.maps.android.ktx.addMarker
27
- import com.google.maps.android.ktx.awaitMap
26
+ import com.google.android.gms.maps.model.MarkerOptions
28
27
29
28
/* *
30
29
* This shows how to create a simple activity with a map and a marker on the map.
31
30
*/
32
31
class BasicMapDemoActivity :
33
- AppCompatActivity () {
32
+ AppCompatActivity (),
33
+ OnMapReadyCallback {
34
34
35
35
val SYDNEY = LatLng (- 33.862 , 151.21 )
36
36
val ZOOM_LEVEL = 13f
37
37
38
- @MapsExperimentalFeature
39
38
override fun onCreate (savedInstanceState : Bundle ? ) {
40
39
super .onCreate(savedInstanceState)
41
40
setContentView(R .layout.activity_basic_map_demo)
42
- val mapFragment = supportFragmentManager.findFragmentById(R .id.map) as ? SupportMapFragment
41
+ val mapFragment : SupportMapFragment ? =
42
+ supportFragmentManager.findFragmentById(R .id.map) as ? SupportMapFragment
43
+ mapFragment?.getMapAsync(this )
44
+ }
43
45
44
- lifecycle.coroutineScope.launchWhenCreated {
45
- val googleMap = mapFragment?.awaitMap() // Execution pauses here until we get the callback from the Maps SDK with a googleMap.
46
- // This is where we can add markers or lines, add listeners or move the camera.
47
- // In this case, we just move the camera to Sydney and add a marker in Sydney.
48
- with (googleMap) {
49
- this ?.moveCamera(CameraUpdateFactory .newLatLngZoom(SYDNEY , ZOOM_LEVEL ))
50
- this ?.addMarker {
51
- position(SYDNEY )
52
- }
53
- }
46
+ /* *
47
+ * This is where we can add markers or lines, add listeners or move the camera. In this case,
48
+ * we just move the camera to Sydney and add a marker in Sydney.
49
+ */
50
+ override fun onMapReady (googleMap : GoogleMap ? ) {
51
+ googleMap ? : return
52
+ with (googleMap) {
53
+ moveCamera(CameraUpdateFactory .newLatLngZoom(SYDNEY , ZOOM_LEVEL ))
54
+ addMarker(MarkerOptions ().position(SYDNEY ))
54
55
}
55
56
}
56
57
}
0 commit comments