Skip to content

Commit e5203ce

Browse files
committed
feat(overlay): modernize GroundOverlay camera framing with LatLngBounds and default initial transparency
1 parent a68b457 commit e5203ce

2 files changed

Lines changed: 19 additions & 11 deletions

File tree

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

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import com.google.android.gms.maps.model.GroundOverlay;
2424
import com.google.android.gms.maps.model.GroundOverlayOptions;
2525
import com.google.android.gms.maps.model.LatLng;
26+
import com.google.android.gms.maps.model.LatLngBounds;
2627

2728
import android.graphics.Point;
2829
import android.os.Bundle;
@@ -51,6 +52,10 @@ public class GroundOverlayDemoActivity extends SamplesBaseActivity
5152
private static final LatLng NEWARK = new LatLng(40.714086, -74.228697);
5253
private static final LatLng NEAR_NEWARK =
5354
new LatLng(NEWARK.latitude - 0.001, NEWARK.longitude - 0.025);
55+
static final LatLngBounds OVERLAY_BOUNDS = new LatLngBounds(
56+
new LatLng(40.7050, -74.2600),
57+
new LatLng(40.7800, -74.1200)
58+
);
5459

5560
private final List<BitmapDescriptor> images = new ArrayList<>();
5661

@@ -84,7 +89,7 @@ protected void onCreate(Bundle savedInstanceState) {
8489
setContentView(binding.getRoot());
8590

8691
binding.transparencySeekBar.setMax(TRANSPARENCY_MAX);
87-
binding.transparencySeekBar.setProgress(0);
92+
binding.transparencySeekBar.setProgress(25);
8893

8994
// Set up programmatic click listeners for the buttons.
9095
binding.switchImage.setOnClickListener(v -> switchImage());
@@ -106,10 +111,8 @@ public void onMapReady(GoogleMap map) {
106111
// Register a listener to respond to clicks on GroundOverlays.
107112
map.setOnGroundOverlayClickListener(this);
108113

109-
// Move the camera to the Newark area.
110-
map.moveCamera(CameraUpdateFactory.newLatLngZoom(NEWARK, 11));
111-
112-
map.moveCamera(CameraUpdateFactory.scrollBy(100f, 100f));
114+
// Move the camera to frame the Newark overlays with padding.
115+
map.moveCamera(CameraUpdateFactory.newLatLngBounds(OVERLAY_BOUNDS, 80));
113116

114117
map.setOnMapClickListener(ll -> {
115118
Point point = mMap.getProjection().toScreenLocation(ll);
@@ -141,7 +144,8 @@ public void onMapReady(GoogleMap map) {
141144
// Add a large overlay at Newark on top of the smaller overlay.
142145
groundOverlay = map.addGroundOverlay(new GroundOverlayOptions()
143146
.image(images.get(currentEntry)).anchor(0, 1)
144-
.position(NEWARK, 8600f, 6500f));
147+
.position(NEWARK, 8600f, 6500f)
148+
.transparency((float) binding.transparencySeekBar.getProgress() / (float) TRANSPARENCY_MAX));
145149
groundOverlay.setTag(images.get(currentEntry));
146150

147151
binding.transparencySeekBar.setOnSeekBarChangeListener(this);

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

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import com.google.android.gms.maps.model.BitmapDescriptorFactory
2929
import com.google.android.gms.maps.model.GroundOverlay
3030
import com.google.android.gms.maps.model.GroundOverlayOptions
3131
import com.google.android.gms.maps.model.LatLng
32+
import com.google.android.gms.maps.model.LatLngBounds
3233

3334
/**
3435
* This demo shows how to add a ground overlay to a map.
@@ -63,7 +64,7 @@ class GroundOverlayDemoActivity : SamplesBaseActivity(),
6364
setContentView(binding.root)
6465

6566
binding.transparencySeekBar.max = TRANSPARENCY_MAX
66-
binding.transparencySeekBar.progress = 0
67+
binding.transparencySeekBar.progress = 25
6768

6869
// Set up programmatic click listeners for the buttons.
6970
// This is a better practice than using the android:onClick XML attribute, as it keeps
@@ -90,10 +91,8 @@ class GroundOverlayDemoActivity : SamplesBaseActivity(),
9091
// Register a listener to respond to clicks on GroundOverlays.
9192
map.setOnGroundOverlayClickListener(this)
9293

93-
// Move the camera to the Newark area.
94-
map.moveCamera(CameraUpdateFactory.newLatLngZoom(NEWARK, 11f))
95-
96-
map.moveCamera(CameraUpdateFactory.scrollBy(100f, 100f))
94+
// Move the camera to frame the Newark overlays with padding.
95+
map.moveCamera(CameraUpdateFactory.newLatLngBounds(OVERLAY_BOUNDS, 80))
9796

9897
// Prepare the BitmapDescriptor objects. Using a BitmapDescriptorFactory is the most
9998
// memory-efficient way to create the images that will be used for the overlays.
@@ -124,6 +123,7 @@ class GroundOverlayDemoActivity : SamplesBaseActivity(),
124123
GroundOverlayOptions()
125124
.image(images[currentEntry]).anchor(0f, 1f)
126125
.position(NEWARK, 8600f, 6500f)
126+
.transparency(binding.transparencySeekBar.progress.toFloat() / TRANSPARENCY_MAX.toFloat())
127127
) ?: error("Expected a non null addGroundOverlay")
128128

129129
groundOverlay.tag = images[currentEntry]
@@ -187,5 +187,9 @@ class GroundOverlayDemoActivity : SamplesBaseActivity(),
187187
NEWARK.latitude - 0.001,
188188
NEWARK.longitude - 0.025
189189
)
190+
internal val OVERLAY_BOUNDS = LatLngBounds(
191+
LatLng(40.7050, -74.2600),
192+
LatLng(40.7800, -74.1200)
193+
)
190194
}
191195
}

0 commit comments

Comments
 (0)