Skip to content

Commit a68b457

Browse files
committed
feat(dataset): use LatLngBounds with padding to frame dataset layers across Kotlin and Java demos
1 parent acd2e02 commit a68b457

2 files changed

Lines changed: 51 additions & 29 deletions

File tree

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

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
import com.google.android.gms.maps.model.FeatureStyle;
4141
import com.google.android.gms.maps.model.FeatureType;
4242
import com.google.android.gms.maps.model.LatLng;
43+
import com.google.android.gms.maps.model.LatLngBounds;
4344
import com.google.android.gms.maps.model.MapCapabilities;
4445

4546
import java.util.List;
@@ -58,8 +59,7 @@ public class DataDrivenDatasetStylingActivity extends SamplesBaseActivity implem
5859
private record DataSet(
5960
String label,
6061
String datasetId,
61-
LatLng location,
62-
float zoomLevel,
62+
LatLngBounds bounds,
6363
DataDrivenDatasetStylingActivity.DataSet.StylingCallback callback) {
6464
public interface StylingCallback {
6565
void styleDatasetLayer();
@@ -71,25 +71,19 @@ public interface StylingCallback {
7171
* Each DataSet contains:
7272
* - A human-readable name (e.g., "Boulder", "New York").
7373
* - A unique Dataset ID, which should correspond to a dataset id in the Datasets console tab.
74-
* - The central latitude and longitude coordinates (LatLng) of the location.
74+
* - The LatLngBounds of the dataset area.
7575
* - A styling function (method reference) that defines how to style the data from that dataset on a map.
76-
* <p>
77-
* This array is used to configure which datasets are available for display and how they should be presented.
78-
* Each element of the array should be a new DataSet object.
79-
* Modify the constructor arguments of each DataSet to define your specific data and styles.
80-
* The styling function will receive a `Layer` to which it can add elements.
81-
* <p>
82-
* Example:
83-
* - `new DataSet("Boulder", "Boulder-DataSet-Id", new LatLng(40.0150, -105.2705), this::styleBoulderDatasetLayer)`
84-
* This creates a DataSet for Boulder, identified by "Boulder-DataSet-Id", centered on the given coordinates,
85-
* and styled using the `styleBoulderDatasetLayer` method.
86-
* <p>
87-
* Note: We have use the secrets plugin to allow us to configure the Dataset IDs in our secrets.properties file.
8876
*/
8977
private final DataSet[] dataSets = new DataSet[] {
90-
new DataSet("Boulder", BuildConfig.BOULDER_DATASET_ID, new LatLng(40.0150, -105.2705), 11f, this::styleBoulderDatasetLayer),
91-
new DataSet("New York", BuildConfig.NEW_YORK_DATASET_ID, new LatLng(40.786244, -73.962684), 14f, this::styleNYCDatasetLayer),
92-
new DataSet("Kyoto", BuildConfig.KYOTO_DATASET_ID, new LatLng(35.005081, 135.764385), 13.5f, this::styleKyotoDatasetsLayer),
78+
new DataSet("Boulder", BuildConfig.BOULDER_DATASET_ID,
79+
new LatLngBounds(new LatLng(39.920, -105.340), new LatLng(40.090, -105.210)),
80+
this::styleBoulderDatasetLayer),
81+
new DataSet("New York", BuildConfig.NEW_YORK_DATASET_ID,
82+
new LatLngBounds(new LatLng(40.7640, -73.9820), new LatLng(40.8000, -73.9490)),
83+
this::styleNYCDatasetLayer),
84+
new DataSet("Kyoto", BuildConfig.KYOTO_DATASET_ID,
85+
new LatLngBounds(new LatLng(34.9700, 135.7200), new LatLng(35.0400, 135.8000)),
86+
this::styleKyotoDatasetsLayer),
9387
};
9488

9589
private DataSet findDataSetByLabel(String label) {
@@ -176,14 +170,19 @@ private void switchDataSet(String label) {
176170
.build()
177171
);
178172
dataSet.callback.styleDatasetLayer();
179-
centerMapOnLocation(dataSet.location(), dataSet.zoomLevel());
173+
centerMapOnBounds(dataSet.bounds());
180174
}
181175
}
182176

183177
@Override
184178
public void onMapReady(@NonNull GoogleMap googleMap) {
185179
this.map = googleMap;
186180

181+
googleMap.setOnCameraIdleListener(() -> {
182+
com.google.android.gms.maps.model.CameraPosition cp = googleMap.getCameraPosition();
183+
Log.i(TAG, "CAMERA_PARAMS: target=LatLng(" + cp.target.latitude + ", " + cp.target.longitude + "), zoom=" + cp.zoom + "f, tilt=" + cp.tilt + "f, bearing=" + cp.bearing + "f");
184+
});
185+
187186
MapCapabilities capabilities = map.getMapCapabilities();
188187
Log.d(TAG, "Data-driven Styling is available: " + capabilities.isDataDrivenStylingAvailable());
189188
if (!capabilities.isDataDrivenStylingAvailable()) {
@@ -364,8 +363,8 @@ private void styleBoulderDatasetLayer() {
364363
}
365364

366365

367-
private void centerMapOnLocation(LatLng location, float zoomLevel) {
368-
map.moveCamera(CameraUpdateFactory.newLatLngZoom(location, zoomLevel));
366+
private void centerMapOnBounds(LatLngBounds bounds) {
367+
map.animateCamera(CameraUpdateFactory.newLatLngBounds(bounds, 80));
369368
}
370369

371370
@Override

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

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ import com.google.android.gms.maps.model.FeatureLayerOptions
4040
import com.google.android.gms.maps.model.FeatureStyle
4141
import com.google.android.gms.maps.model.FeatureType
4242
import com.google.android.gms.maps.model.LatLng
43+
import com.google.android.gms.maps.model.LatLngBounds
4344
import com.google.android.gms.maps.model.MapCapabilities
4445
import androidx.core.view.WindowCompat
4546
import com.google.android.gms.maps.GoogleMapOptions
@@ -56,7 +57,6 @@ class DataDrivenDatasetStylingActivity : SamplesBaseActivity(), OnMapReadyCallba
5657
private lateinit var mapContainer: ViewGroup
5758

5859
private lateinit var map: GoogleMap
59-
private val zoomLevel = 13.5f
6060

6161
private var datasetLayer: FeatureLayer? = null
6262

@@ -65,7 +65,7 @@ class DataDrivenDatasetStylingActivity : SamplesBaseActivity(), OnMapReadyCallba
6565

6666
private data class DataSet(
6767
val datasetId: String,
68-
val location: LatLng,
68+
val bounds: LatLngBounds,
6969
val callback: DataDrivenDatasetStylingActivity.() -> Unit
7070
)
7171

@@ -87,9 +87,27 @@ class DataDrivenDatasetStylingActivity : SamplesBaseActivity(), OnMapReadyCallba
8787

8888
if (dataSets.isEmpty()) {
8989
with(dataSets) {
90-
put(getString(com.example.common_ui.R.string.boulder), DataSet(BuildConfig.BOULDER_DATASET_ID, LatLng(40.0150, -105.2705)) { styleBoulderDataset() })
91-
put(getString(com.example.common_ui.R.string.new_york), DataSet(BuildConfig.NEW_YORK_DATASET_ID, LatLng(40.786244, -73.962684)) { styleNYCDataset() })
92-
put(getString(com.example.common_ui.R.string.kyoto), DataSet(BuildConfig.KYOTO_DATASET_ID, LatLng(35.005081, 135.764385)) { styleKyotoDataset() })
90+
put(
91+
getString(com.example.common_ui.R.string.boulder),
92+
DataSet(
93+
BuildConfig.BOULDER_DATASET_ID,
94+
LatLngBounds(LatLng(39.920, -105.340), LatLng(40.090, -105.210))
95+
) { styleBoulderDataset() }
96+
)
97+
put(
98+
getString(com.example.common_ui.R.string.new_york),
99+
DataSet(
100+
BuildConfig.NEW_YORK_DATASET_ID,
101+
LatLngBounds(LatLng(40.7640, -73.9820), LatLng(40.8000, -73.9490))
102+
) { styleNYCDataset() }
103+
)
104+
put(
105+
getString(com.example.common_ui.R.string.kyoto),
106+
DataSet(
107+
BuildConfig.KYOTO_DATASET_ID,
108+
LatLngBounds(LatLng(34.9700, 135.7200), LatLng(35.0400, 135.8000))
109+
) { styleKyotoDataset() }
110+
)
93111
}
94112
}
95113

@@ -172,7 +190,7 @@ class DataDrivenDatasetStylingActivity : SamplesBaseActivity(), OnMapReadyCallba
172190
}.build()
173191
)
174192
dataSet.callback(this)
175-
centerMapOnLocation(dataSet.location)
193+
centerMapOnBounds(dataSet.bounds)
176194
} ?: run {
177195
Toast.makeText(this, "Unknown dataset: $label", Toast.LENGTH_SHORT).show()
178196
}
@@ -181,6 +199,11 @@ class DataDrivenDatasetStylingActivity : SamplesBaseActivity(), OnMapReadyCallba
181199
override fun onMapReady(googleMap: GoogleMap) {
182200
map = googleMap
183201

202+
googleMap.setOnCameraIdleListener {
203+
val cp = googleMap.cameraPosition
204+
Log.i(TAG, "CAMERA_PARAMS: target=LatLng(${cp.target.latitude}, ${cp.target.longitude}), zoom=${cp.zoom}f, tilt=${cp.tilt}f, bearing=${cp.bearing}f")
205+
}
206+
184207
val capabilities: MapCapabilities = map.mapCapabilities
185208
println("Data-driven Styling is available: " + capabilities.isDataDrivenStylingAvailable)
186209

@@ -339,8 +362,8 @@ class DataDrivenDatasetStylingActivity : SamplesBaseActivity(), OnMapReadyCallba
339362
}
340363

341364

342-
private fun centerMapOnLocation(location: LatLng) {
343-
map.moveCamera(CameraUpdateFactory.newLatLngZoom(location, zoomLevel))
365+
private fun centerMapOnBounds(bounds: LatLngBounds, padding: Int = 80) {
366+
map.animateCamera(CameraUpdateFactory.newLatLngBounds(bounds, padding))
344367
}
345368

346369
// Define the click event handler to set lastGlobalId to globalid of selected feature.

0 commit comments

Comments
 (0)