Skip to content

Instantly share code, notes, and snippets.

@spss20
Created April 20, 2024 07:00
Show Gist options
  • Save spss20/b1b9781889113c6d3863846204c85412 to your computer and use it in GitHub Desktop.
Save spss20/b1b9781889113c6d3863846204c85412 to your computer and use it in GitHub Desktop.
Marker Clustering Jetpack Compose
package com.ssoftwares.chargepe
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.size
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.mutableStateListOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.google.android.gms.maps.model.CameraPosition
import com.google.android.gms.maps.model.LatLng
import com.google.maps.android.clustering.ClusterItem
import com.google.maps.android.compose.GoogleMap
import com.google.maps.android.compose.MapsComposeExperimentalApi
import com.google.maps.android.compose.clustering.Clustering
import com.google.maps.android.compose.rememberCameraPositionState
import com.ssoftwares.chargepe.ui.theme.ChargePeTheme
import kotlin.random.Random
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
enableEdgeToEdge()
super.onCreate(savedInstanceState)
setContent {
ChargePeTheme {
// A surface container using the 'background' color from the theme
Box(
modifier = Modifier
.fillMaxSize()
) {
MainView(modifier = Modifier.matchParentSize())
}
}
}
}
}
@OptIn(MapsComposeExperimentalApi::class)
@Composable
fun MainView(modifier: Modifier = Modifier) {
val cameraPosition = rememberCameraPositionState {
position = CameraPosition.fromLatLngZoom(getRandomPosition(), 9f)
}
val stationsList = remember {
mutableStateListOf<StationItem>()
}
println("Recomposition")
LaunchedEffect(Unit) {
for (i in 1..700) {
val position = getRandomPosition();
stationsList.add(
StationItem(
position, "Marker $i",
"Snippet", 0f
)
)
}
}
GoogleMap(modifier = modifier, cameraPositionState = cameraPosition) {
Clustering(items = stationsList)
}
}
fun getRandomPosition(): LatLng {
return LatLng(
28.354945 + Random.nextFloat(),
76.948420 + Random.nextFloat()
);
}
@Preview(showBackground = true)
@Composable
fun GreetingPreview() {
ChargePeTheme {
MainView(Modifier.size(200.dp))
}
}
data class StationItem(
val itemPosition: LatLng,
val itemTitle: String,
val itemSnippet: String,
val itemZIndex: Float
) : ClusterItem {
override fun getPosition(): LatLng = itemPosition
override fun getTitle(): String = itemTitle
override fun getSnippet(): String = itemSnippet
override fun getZIndex(): Float = itemZIndex
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment