@@ -21,7 +21,9 @@ import android.util.Log
2121import com.google.android.gms.maps.SupportMapFragment
2222import com.google.android.gms.maps.GoogleMap
2323import android.widget.TextView
24+ import com.example.common_ui.R
2425import com.google.android.gms.maps.CameraUpdateFactory
26+ import com.google.android.gms.maps.GoogleMapOptions
2527import com.google.android.gms.maps.model.AdvancedMarkerOptions
2628import com.google.android.gms.maps.model.BitmapDescriptorFactory
2729import com.google.android.gms.maps.model.LatLng
@@ -49,11 +51,87 @@ private val TAG = AdvancedMarkersDemoActivity::class.java.name
4951// [START maps_android_sample_marker_advanced]
5052class AdvancedMarkersDemoActivity : SamplesBaseActivity (), OnMapReadyCallback {
5153
54+ /* *
55+ * This method is called when the activity is first created.
56+ *
57+ * It sets up the activity's layout and then initializes the map.
58+ *
59+ * The key logic here is to check if the developer has provided a Map ID in the
60+ * `strings.xml` file.
61+ *
62+ * If the `R.string.map_id` value is not the default "DEMO_MAP_ID", it means a
63+ * custom Map ID has been provided. In this case, we can rely on the simpler setup
64+ * where the `SupportMapFragment` is inflated directly from the XML layout, and it
65+ * will automatically use the Map ID from the string resource.
66+ *
67+ * However, if the `R.string.map_id` is still the default value, we fall back to a
68+ * programmatic setup. This involves:
69+ * 1. Retrieving the Map ID from the `secrets.properties` file, which is managed by the
70+ * `ApiDemoApplication` class.
71+ * 2. Creating a `GoogleMapOptions` object.
72+ * 3. Explicitly setting the retrieved `mapId` on the `GoogleMapOptions`. This step is
73+ * **critical** because Advanced Markers will not work without a valid Map ID.
74+ * 4. Creating a new `SupportMapFragment` instance with these options and replacing the
75+ * placeholder fragment in the layout.
76+ *
77+ * This dual approach ensures that the demo can run seamlessly while also providing a
78+ * clear path for developers to use their own Map IDs, which is a requirement for using
79+ * Advanced Markers.
80+ */
81+ /* *
82+ * This method is called when the activity is first created.
83+ *
84+ * It sets up the activity's layout and then initializes the map.
85+ *
86+ * The key logic here is to check if the developer has provided a Map ID in the
87+ * `strings.xml` file.
88+ *
89+ * If the `R.string.map_id` value is not the default "DEMO_MAP_ID", it means a
90+ * custom Map ID has been provided. In this case, we can rely on the simpler setup
91+ * where the `SupportMapFragment` is inflated directly from the XML layout, and it
92+ * will automatically use the Map ID from the string resource.
93+ *
94+ * However, if the `R.string.map_id` is still the default value, we fall back to a
95+ * programmatic setup. This involves:
96+ * 1. Retrieving the Map ID from the `secrets.properties` file, via the
97+ * `ApiDemoApplication.mapId` property.
98+ * 2. Creating a `GoogleMapOptions` object.
99+ * 3. Explicitly setting the retrieved `mapId` on the `GoogleMapOptions`. This step is
100+ * **critical** because Advanced Markers will not work without a valid Map ID.
101+ * 4. Creating a new `SupportMapFragment` instance with these options and replacing the
102+ * placeholder fragment in the layout.
103+ *
104+ * This dual approach ensures that the demo can run seamlessly while also providing a
105+ * clear path for developers to use their own Map IDs, which is a requirement for using
106+ * Advanced Markers.
107+ */
52108 override fun onCreate (savedInstanceState : Bundle ? ) {
53109 super .onCreate(savedInstanceState)
54110 setContentView(com.example.common_ui.R .layout.advanced_markers_demo)
55- val mapFragment = supportFragmentManager.findFragmentById(com.example.common_ui.R .id.map) as SupportMapFragment ?
56- mapFragment?.getMapAsync(this )
111+
112+ if (getString(com.example.common_ui.R .string.map_id) != " DEMO_MAP_ID" ) {
113+ val mapFragment = supportFragmentManager.findFragmentById(com.example.common_ui.R .id.map) as SupportMapFragment ?
114+ mapFragment?.getMapAsync(this )
115+ } else {
116+ val mapId = (application as ApiDemoApplication ).mapId
117+
118+ // --- Map ID Check ---
119+ if (mapId == null ) {
120+ finish()
121+ return // Exit early if no valid Map ID
122+ }
123+
124+ // --- Programmatically create and add the map fragment ---
125+ val mapOptions = GoogleMapOptions ().apply {
126+ mapId(mapId)
127+ }
128+ val mapFragment = SupportMapFragment .newInstance(mapOptions)
129+ supportFragmentManager.beginTransaction()
130+ .replace(R .id.map, mapFragment) // Use the container ID
131+ .commit()
132+ mapFragment.getMapAsync(this )
133+ }
134+
57135 applyInsets(findViewById(com.example.common_ui.R .id.map_container))
58136 }
59137
0 commit comments