|
| 1 | +// Copyright 2026 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package com.google.maps.android.compose |
| 16 | + |
| 17 | +import android.os.Bundle |
| 18 | +import java.util.Locale |
| 19 | +import androidx.activity.ComponentActivity |
| 20 | +import androidx.activity.compose.setContent |
| 21 | +import androidx.activity.enableEdgeToEdge |
| 22 | +import androidx.compose.foundation.layout.Box |
| 23 | +import androidx.compose.foundation.layout.Column |
| 24 | +import androidx.compose.foundation.layout.Row |
| 25 | +import androidx.compose.foundation.layout.fillMaxSize |
| 26 | +import androidx.compose.foundation.layout.fillMaxWidth |
| 27 | +import androidx.compose.foundation.layout.padding |
| 28 | +import androidx.compose.foundation.layout.systemBarsPadding |
| 29 | +import androidx.compose.material3.MaterialTheme |
| 30 | +import androidx.compose.material3.Slider |
| 31 | +import androidx.compose.material3.Surface |
| 32 | +import androidx.compose.material3.Switch |
| 33 | +import androidx.compose.material3.Text |
| 34 | +import androidx.compose.runtime.Composable |
| 35 | +import androidx.compose.runtime.getValue |
| 36 | +import androidx.compose.runtime.mutableFloatStateOf |
| 37 | +import androidx.compose.runtime.mutableStateOf |
| 38 | +import androidx.compose.runtime.remember |
| 39 | +import androidx.compose.runtime.setValue |
| 40 | +import androidx.compose.ui.Alignment |
| 41 | +import androidx.compose.ui.Modifier |
| 42 | +import androidx.compose.ui.unit.dp |
| 43 | +import com.google.android.gms.maps.model.BitmapDescriptorFactory |
| 44 | +import com.google.android.gms.maps.model.LatLng |
| 45 | +import com.google.android.gms.maps.model.LatLngBounds |
| 46 | +import com.google.maps.android.compose.theme.MapsComposeSampleTheme |
| 47 | +import androidx.core.graphics.createBitmap |
| 48 | + |
| 49 | +class GroundOverlayActivity : ComponentActivity() { |
| 50 | + |
| 51 | + override fun onCreate(savedInstanceState: Bundle?) { |
| 52 | + super.onCreate(savedInstanceState) |
| 53 | + enableEdgeToEdge() |
| 54 | + setContent { |
| 55 | + MapsComposeSampleTheme { |
| 56 | + GroundOverlayScreen() |
| 57 | + } |
| 58 | + } |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +@Composable |
| 63 | +fun GroundOverlayScreen() { |
| 64 | + val singapore = LatLng(1.3588227, 103.8742114) |
| 65 | + val cameraPositionState = rememberCameraPositionState { |
| 66 | + position = com.google.android.gms.maps.model.CameraPosition.fromLatLngZoom(singapore, 12f) |
| 67 | + } |
| 68 | + |
| 69 | + var isVisible by remember { mutableStateOf(true) } |
| 70 | + var transparency by remember { mutableFloatStateOf(0f) } |
| 71 | + var bearing by remember { mutableFloatStateOf(0f) } |
| 72 | + |
| 73 | + val context = androidx.compose.ui.platform.LocalContext.current |
| 74 | + val imageDescriptor = remember { |
| 75 | + val drawable = androidx.core.content.ContextCompat.getDrawable(context, R.mipmap.ic_launcher) |
| 76 | + val bitmap = createBitmap(drawable!!.intrinsicWidth, drawable.intrinsicHeight) |
| 77 | + val canvas = android.graphics.Canvas(bitmap) |
| 78 | + drawable.setBounds(0, 0, canvas.width, canvas.height) |
| 79 | + drawable.draw(canvas) |
| 80 | + BitmapDescriptorFactory.fromBitmap(bitmap) |
| 81 | + } |
| 82 | + |
| 83 | + Box( |
| 84 | + modifier = Modifier |
| 85 | + .fillMaxSize() |
| 86 | + .systemBarsPadding() |
| 87 | + ) { |
| 88 | + GoogleMap( |
| 89 | + modifier = Modifier.fillMaxSize(), |
| 90 | + cameraPositionState = cameraPositionState |
| 91 | + ) { |
| 92 | + // Stable GroundOverlay (using remembered state) |
| 93 | + GroundOverlay( |
| 94 | + position = GroundOverlayPosition.create( |
| 95 | + LatLngBounds( |
| 96 | + LatLng(1.35, 103.86), |
| 97 | + LatLng(1.37, 103.88) |
| 98 | + ) |
| 99 | + ), |
| 100 | + image = imageDescriptor, |
| 101 | + visible = isVisible, |
| 102 | + transparency = transparency, |
| 103 | + bearing = bearing |
| 104 | + ) |
| 105 | + |
| 106 | + // Stress-test GroundOverlay: Re-creating position every recomposition |
| 107 | + // This would cause a crash/rendering loop if GroundOverlayPosition was not a data class |
| 108 | + GroundOverlay( |
| 109 | + position = GroundOverlayPosition.create( |
| 110 | + LatLngBounds( |
| 111 | + LatLng(1.36, 103.89), |
| 112 | + LatLng(1.38, 103.91) |
| 113 | + ) |
| 114 | + ), |
| 115 | + image = imageDescriptor, |
| 116 | + transparency = 0.5f, |
| 117 | + zIndex = 1f |
| 118 | + ) |
| 119 | + } |
| 120 | + |
| 121 | + Column( |
| 122 | + modifier = Modifier |
| 123 | + .align(Alignment.BottomCenter) |
| 124 | + .fillMaxWidth() |
| 125 | + .padding(16.dp) |
| 126 | + ) { |
| 127 | + GroundOverlayControls( |
| 128 | + isVisible = isVisible, |
| 129 | + onVisibilityChange = { isVisible = it }, |
| 130 | + transparency = transparency, |
| 131 | + onTransparencyChange = { transparency = it }, |
| 132 | + bearing = bearing, |
| 133 | + onBearingChange = { bearing = it } |
| 134 | + ) |
| 135 | + } |
| 136 | + } |
| 137 | +} |
| 138 | + |
| 139 | +@Composable |
| 140 | +fun GroundOverlayControls( |
| 141 | + isVisible: Boolean, |
| 142 | + onVisibilityChange: (Boolean) -> Unit, |
| 143 | + transparency: Float, |
| 144 | + onTransparencyChange: (Float) -> Unit, |
| 145 | + bearing: Float, |
| 146 | + onBearingChange: (Float) -> Unit |
| 147 | +) { |
| 148 | + Surface( |
| 149 | + shape = MaterialTheme.shapes.medium, |
| 150 | + tonalElevation = 4.dp, |
| 151 | + color = MaterialTheme.colorScheme.surface.copy(alpha = 0.8f), |
| 152 | + modifier = Modifier.fillMaxWidth() |
| 153 | + ) { |
| 154 | + Column(modifier = Modifier.padding(16.dp)) { |
| 155 | + Row(verticalAlignment = Alignment.CenterVertically) { |
| 156 | + Text(text = "Visible", modifier = Modifier.weight(1f)) |
| 157 | + Switch(checked = isVisible, onCheckedChange = onVisibilityChange) |
| 158 | + } |
| 159 | + Text(text = "Transparency: ${String.format(Locale.getDefault(), "%.2f", transparency)}") |
| 160 | + Slider( |
| 161 | + value = transparency, |
| 162 | + onValueChange = onTransparencyChange, |
| 163 | + valueRange = 0f..1f |
| 164 | + ) |
| 165 | + Text(text = "Bearing: ${bearing.toInt()}°") |
| 166 | + Slider( |
| 167 | + value = bearing, |
| 168 | + onValueChange = onBearingChange, |
| 169 | + valueRange = 0f..360f |
| 170 | + ) |
| 171 | + } |
| 172 | + } |
| 173 | +} |
0 commit comments