Skip to content

Commit 092346f

Browse files
authored
feat: Use Map ID from secrets for Advanced Markers (#2302)
Updates the Advanced Markers demo to programmatically initialize the map when a custom Map ID is not provided in the string resources. This ensures that a valid Map ID from the secrets file is always used, which is a requirement for Advanced Markers to function correctly. This change includes: - Adding detailed comments to both the Java and Kotlin versions of the activity to explain the Map ID loading logic. - Implementing a fallback mechanism to create the `SupportMapFragment` with a `GoogleMapOptions` object that explicitly sets the Map ID.
1 parent ffabd30 commit 092346f

File tree

2 files changed

+126
-4
lines changed

2 files changed

+126
-4
lines changed

ApiDemos/project/java-app/src/main/java/com/example/mapdemo/AdvancedMarkersDemoActivity.java

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
import com.google.android.gms.maps.CameraUpdateFactory;
2929
import com.google.android.gms.maps.GoogleMap;
30+
import com.google.android.gms.maps.GoogleMapOptions;
3031
import com.google.android.gms.maps.OnMapReadyCallback;
3132
import com.google.android.gms.maps.SupportMapFragment;
3233
import com.google.android.gms.maps.model.AdvancedMarkerOptions;
@@ -54,15 +55,58 @@ public class AdvancedMarkersDemoActivity extends SamplesBaseActivity implements
5455

5556
private static final String TAG = AdvancedMarkersDemoActivity.class.getName();
5657

58+
/**
59+
* This method is called when the activity is first created.
60+
*
61+
* It sets up the activity's layout and then initializes the map.
62+
*
63+
* The key logic here is to check if the developer has provided a Map ID in the
64+
* `strings.xml` file.
65+
*
66+
* If the `R.string.map_id` value is not the default "DEMO_MAP_ID", it means a
67+
* custom Map ID has been provided. In this case, we can rely on the simpler setup
68+
* where the `SupportMapFragment` is inflated directly from the XML layout, and it
69+
* will automatically use the Map ID from the string resource.
70+
*
71+
* However, if the `R.string.map_id` is still the default value, we fall back to a
72+
* programmatic setup. This involves:
73+
* 1. Retrieving the Map ID from the `secrets.properties` file, which is managed by the
74+
* `ApiDemoApplication` class.
75+
* 2. Creating a `GoogleMapOptions` object.
76+
* 3. Explicitly setting the retrieved `mapId` on the `GoogleMapOptions`. This step is
77+
* **critical** because Advanced Markers will not work without a valid Map ID.
78+
* 4. Creating a new `SupportMapFragment` instance with these options and replacing the
79+
* placeholder fragment in the layout.
80+
*
81+
* This dual approach ensures that the demo can run seamlessly while also providing a
82+
* clear path for developers to use their own Map IDs, which is a requirement for using
83+
* Advanced Markers.
84+
*/
5785
@Override
5886
protected void onCreate(Bundle savedInstanceState) {
5987
super.onCreate(savedInstanceState);
6088
setContentView(com.example.common_ui.R.layout.advanced_markers_demo);
6189

62-
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(com.example.common_ui.R.id.map);
63-
if (mapFragment != null) {
90+
if (!getString(com.example.common_ui.R.string.map_id).equals("DEMO_MAP_ID")) {
91+
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(com.example.common_ui.R.id.map);
92+
if (mapFragment != null) {
93+
mapFragment.getMapAsync(this);
94+
}
95+
} else {
96+
String mapId = ((ApiDemoApplication) getApplication()).getMapId();
97+
if (mapId == null) {
98+
finish();
99+
return;
100+
}
101+
102+
GoogleMapOptions mapOptions = new GoogleMapOptions().mapId(mapId);
103+
SupportMapFragment mapFragment = SupportMapFragment.newInstance(mapOptions);
104+
getSupportFragmentManager().beginTransaction()
105+
.replace(com.example.common_ui.R.id.map, mapFragment)
106+
.commit();
64107
mapFragment.getMapAsync(this);
65108
}
109+
66110
applyInsets(findViewById(com.example.common_ui.R.id.map_container));
67111
}
68112

ApiDemos/project/kotlin-app/src/main/java/com/example/kotlindemos/AdvancedMarkersDemoActivity.kt

Lines changed: 80 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ import android.util.Log
2121
import com.google.android.gms.maps.SupportMapFragment
2222
import com.google.android.gms.maps.GoogleMap
2323
import android.widget.TextView
24+
import com.example.common_ui.R
2425
import com.google.android.gms.maps.CameraUpdateFactory
26+
import com.google.android.gms.maps.GoogleMapOptions
2527
import com.google.android.gms.maps.model.AdvancedMarkerOptions
2628
import com.google.android.gms.maps.model.BitmapDescriptorFactory
2729
import 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]
5052
class 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

Comments
 (0)