diff --git a/tutorials/java/MapWithMarker/app/src/main/java/com/example/mapwithmarker/MapsMarkerActivity.java b/tutorials/java/MapWithMarker/app/src/main/java/com/example/mapwithmarker/MapsMarkerActivity.java index 5ed17420..ef8813c5 100644 --- a/tutorials/java/MapWithMarker/app/src/main/java/com/example/mapwithmarker/MapsMarkerActivity.java +++ b/tutorials/java/MapWithMarker/app/src/main/java/com/example/mapwithmarker/MapsMarkerActivity.java @@ -27,9 +27,12 @@ import com.google.android.gms.maps.model.MarkerOptions; /** * An activity that displays a Google map with a marker (pin) to indicate a particular location. */ +// [START maps_marker_on_map_ready] public class MapsMarkerActivity extends AppCompatActivity implements OnMapReadyCallback { + // [START_EXCLUDE] + // [START maps_marker_get_map_async] @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -41,7 +44,10 @@ public class MapsMarkerActivity extends AppCompatActivity .findFragmentById(R.id.map); mapFragment.getMapAsync(this); } + // [END maps_marker_get_map_async] + // [END_EXCLUDE] + // [START_EXCLUDE silent] /** * Manipulates the map when it's available. * The API invokes this callback when the map is ready to be used. @@ -51,6 +57,7 @@ public class MapsMarkerActivity extends AppCompatActivity * Play services inside the SupportMapFragment. The API invokes this method after the user has * installed Google Play services and returned to the app. */ + // [END_EXCLUDE silent] @Override public void onMapReady(GoogleMap googleMap) { // Add a marker in Sydney, Australia, @@ -61,3 +68,4 @@ public class MapsMarkerActivity extends AppCompatActivity googleMap.moveCamera(CameraUpdateFactory.newLatLng(sydney)); } } +// [END maps_marker_on_map_ready] diff --git a/tutorials/kotlin/MapWithMarker/app/src/main/java/com/example/mapwithmarker/MapsMarkerActivity.kt b/tutorials/kotlin/MapWithMarker/app/src/main/java/com/example/mapwithmarker/MapsMarkerActivity.kt index 05d164e6..5fbaf10e 100644 --- a/tutorials/kotlin/MapWithMarker/app/src/main/java/com/example/mapwithmarker/MapsMarkerActivity.kt +++ b/tutorials/kotlin/MapWithMarker/app/src/main/java/com/example/mapwithmarker/MapsMarkerActivity.kt @@ -19,8 +19,11 @@ import android.widget.Toast 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 @@ -28,9 +31,11 @@ import com.google.maps.android.ktx.awaitMap /** * An activity that displays a Google map with a marker (pin) to indicate a particular location. */ -class MapsMarkerActivity : AppCompatActivity() { +// [START maps_marker_on_map_ready] +class MapsMarkerActivity : AppCompatActivity(), OnMapReadyCallback { - @MapsExperimentalFeature + // [START_EXCLUDE] + // [START maps_marker_get_map_async] override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) // Retrieve the content view that renders the map. @@ -41,23 +46,21 @@ class MapsMarkerActivity : AppCompatActivity() { // Get the SupportMapFragment and request notification when the map is ready to be used. val mapFragment = supportFragmentManager.findFragmentById(R.id.map) as? SupportMapFragment + mapFragment?.getMapAsync(this) + } + // [END maps_marker_get_map_async] + // [END_EXCLUDE] - // This is using Android's Lifecycle-aware architecture components to launch and run the - // provided code block so that it executes as soon as the Activity is at least in the - // CREATED state. - lifecycle.coroutineScope.launchWhenCreated { - 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. - googleMap?.apply { - val sydney = LatLng(-33.852, 151.211) - addMarker { - position(sydney) - title("Marker in Sydney") - } - moveCamera(CameraUpdateFactory.newLatLng(sydney)) - } + override fun onMapReady(googleMap: GoogleMap?) { + googleMap?.apply { + val sydney = LatLng(-33.852, 151.211) + addMarker( + MarkerOptions() + .position(sydney) + .title("Marker in Sydney") + ) + moveCamera(CameraUpdateFactory.newLatLng(sydney)) } } } +// [END maps_marker_on_map_ready]