Skip to content

Commit e1c2277

Browse files
kikosodkhawk
andauthored
feat: added BitmapDescriptor as parameter to AdvancedMarker (#718)
* feat: added BitmapDescriptor as parameter to AdvancedMarker * feat: added comment --------- Co-authored-by: Dale Hawkins <[email protected]>
1 parent 57b6776 commit e1c2277

File tree

3 files changed

+42
-1
lines changed

3 files changed

+42
-1
lines changed

maps-app/src/main/java/com/google/maps/android/compose/markerexamples/AdvancedMarkersActivity.kt

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ import com.google.maps.android.compose.MapProperties
4545
import com.google.maps.android.compose.MapType
4646
import com.google.maps.android.compose.rememberCameraPositionState
4747
import com.google.maps.android.compose.rememberUpdatedMarkerState
48+
import com.google.maps.android.ui.IconGenerator
4849

4950

5051
private const val TAG = "AdvancedMarkersActivity"
@@ -53,6 +54,7 @@ private val santiago = LatLng(-33.4489, -70.6693)
5354
private val bogota = LatLng(-4.7110, -74.0721)
5455
private val lima = LatLng(-12.0464, -77.0428)
5556
private val salvador = LatLng(-12.9777, -38.5016)
57+
private val caracas = LatLng(10.4785, -66.9016)
5658
private val center = LatLng(-18.000, -58.000)
5759
private val defaultCameraPosition1 = CameraPosition.fromLatLngZoom(center, 2f)
5860
class AdvancedMarkersActivity : ComponentActivity(), OnMapsSdkInitializedCallback {
@@ -74,6 +76,7 @@ class AdvancedMarkersActivity : ComponentActivity(), OnMapsSdkInitializedCallbac
7476
val marker2State = rememberUpdatedMarkerState(position = bogota)
7577
val marker3State = rememberUpdatedMarkerState(position = lima)
7678
val marker4State = rememberUpdatedMarkerState(position = salvador)
79+
val marker5State = rememberUpdatedMarkerState(position = caracas)
7780

7881
// Drawing on the map is accomplished with a child-based API
7982
val markerClick: (Marker) -> Boolean = {
@@ -112,6 +115,25 @@ class AdvancedMarkersActivity : ComponentActivity(), OnMapsSdkInitializedCallbac
112115
title="Marker 4"
113116
)
114117

118+
val icon = remember {
119+
val iconGenerator = IconGenerator(this@AdvancedMarkersActivity)
120+
val contentView = TextView(this@AdvancedMarkersActivity)
121+
contentView.text = "Caracas"
122+
contentView.setBackgroundColor(Color.BLACK)
123+
contentView.setTextColor(Color.YELLOW)
124+
iconGenerator.setBackground(null)
125+
iconGenerator.setContentView(contentView)
126+
val bitmap = iconGenerator.makeIcon()
127+
BitmapDescriptorFactory.fromBitmap(bitmap)
128+
}
129+
AdvancedMarker(
130+
state = marker5State,
131+
onClick = markerClick,
132+
collisionBehavior = 1,
133+
icon = icon,
134+
title = "Marker 5"
135+
)
136+
115137
val pinConfig = PinConfig.builder()
116138
.setBackgroundColor(Color.MAGENTA)
117139
.setBorderColor(Color.WHITE)

maps-app/src/main/java/com/google/maps/android/compose/markerexamples/MarkerClusteringActivity.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,7 @@ fun CustomRendererClustering(items: List<MyItem>) {
250250
clusterManager = clusterManager,
251251
)
252252
}
253+
253254
}
254255

255256
@Composable

maps-compose/src/main/java/com/google/maps/android/compose/Marker.kt

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -719,6 +719,7 @@ private fun MarkerImpl(
719719
* @param onInfoWindowClick a lambda invoked when the marker's info window is clicked
720720
* @param onInfoWindowClose a lambda invoked when the marker's info window is closed
721721
* @param onInfoWindowLongClick a lambda invoked when the marker's info window is long clicked
722+
* @param icon sets the icon for the marker
722723
* @param pinConfig the PinConfig object that will be used for the advanced marker
723724
* @param iconView the custom view to be used on the advanced marker
724725
* @param collisionBehavior the expected collision behavior
@@ -743,6 +744,7 @@ public fun AdvancedMarker(
743744
onInfoWindowClick: (Marker) -> Unit = {},
744745
onInfoWindowClose: (Marker) -> Unit = {},
745746
onInfoWindowLongClick: (Marker) -> Unit = {},
747+
icon: BitmapDescriptor? = null,
746748
pinConfig: PinConfig? = null,
747749
iconView: View? = null,
748750
collisionBehavior: Int = AdvancedMarkerOptions.CollisionBehavior.REQUIRED
@@ -765,6 +767,7 @@ public fun AdvancedMarker(
765767
onInfoWindowClick = onInfoWindowClick,
766768
onInfoWindowClose = onInfoWindowClose,
767769
onInfoWindowLongClick = onInfoWindowLongClick,
770+
icon = icon,
768771
pinConfig = pinConfig,
769772
iconView = iconView,
770773
collisionBehavior = collisionBehavior
@@ -797,6 +800,7 @@ public fun AdvancedMarker(
797800
* will be ignored.
798801
* @param infoContent optional composable lambda expression for customizing
799802
* the info window's content. If this value is non-null, [infoWindow] must be null.
803+
* @param icon sets the icon for the marker
800804
* @param pinConfig the PinConfig object that will be used for the advanced marker
801805
* @param iconView the custom view to be used on the advanced marker
802806
* @param collisionBehavior the expected collision behavior
@@ -823,6 +827,7 @@ private fun AdvancedMarkerImpl(
823827
onInfoWindowLongClick: (Marker) -> Unit = {},
824828
infoWindow: (@Composable (Marker) -> Unit)? = null,
825829
infoContent: (@Composable (Marker) -> Unit)? = null,
830+
icon: BitmapDescriptor? = null,
826831
pinConfig: PinConfig? = null,
827832
iconView: View? = null,
828833
collisionBehavior: Int = AdvancedMarkerOptions.CollisionBehavior.REQUIRED
@@ -836,10 +841,18 @@ private fun AdvancedMarkerImpl(
836841
val advancedMarkerOptions = AdvancedMarkerOptions()
837842
.position(state.position)
838843
.collisionBehavior(collisionBehavior)
844+
845+
// Determine the icon for the marker in order of precedence:
846+
// 1. Use iconView if provided (takes full precedence and overrides all).
847+
// 2. If no iconView, use pinConfig to generate a BitmapDescriptor.
848+
// 3. If neither iconView nor pinConfig are available, fall back to the raw icon.
849+
839850
if (iconView != null) {
840851
advancedMarkerOptions.iconView(iconView)
841852
} else if (pinConfig != null) {
842853
advancedMarkerOptions.icon(BitmapDescriptorFactory.fromPinConfig(pinConfig))
854+
} else if (icon != null) {
855+
advancedMarkerOptions.icon(icon)
843856
}
844857
advancedMarkerOptions.contentDescription(contentDescription)
845858
advancedMarkerOptions.alpha(alpha)
@@ -897,14 +910,19 @@ private fun AdvancedMarkerImpl(
897910
}
898911
}
899912
update(pinConfig) {
900-
if (iconView == null) {
913+
if (icon == null && iconView == null) {
901914
this.marker.setIcon(pinConfig?.let { it1 ->
902915
BitmapDescriptorFactory.fromPinConfig(
903916
it1
904917
)
905918
})
906919
}
907920
}
921+
update(icon) {
922+
if (iconView == null) {
923+
this.marker.setIcon(it)
924+
}
925+
}
908926

909927
update(visible) { this.marker.isVisible = it }
910928
update(zIndex) { this.marker.zIndex = it }

0 commit comments

Comments
 (0)