Skip to content

Commit 0cfa400

Browse files
committed
docs: comments from PR
1 parent 81dbf3e commit 0cfa400

File tree

2 files changed

+24
-45
lines changed

2 files changed

+24
-45
lines changed

demo/src/main/java/com/google/maps/android/utils/demo/ClusterAlgorithmsDemoActivity.kt

Lines changed: 17 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import com.google.android.gms.maps.CameraUpdateFactory
2424
import com.google.android.gms.maps.MapView
2525
import com.google.android.gms.maps.model.LatLng
2626
import com.google.maps.android.clustering.ClusterManager
27+
import com.google.maps.android.clustering.algo.AbstractAlgorithm
2728
import com.google.maps.android.clustering.algo.CentroidNonHierarchicalDistanceBasedAlgorithm
2829
import com.google.maps.android.clustering.algo.ContinuousZoomEuclideanCentroidAlgorithm
2930
import com.google.maps.android.clustering.algo.GridBasedAlgorithm
@@ -46,14 +47,11 @@ class ClusterAlgorithmsDemoActivity : BaseDemoActivity() {
4647
}
4748

4849
override fun startDemo(isRestore: Boolean) {
49-
// The MapView is needed for the NonHierarchicalViewBasedAlgorithm.
50-
mapView = findViewById(R.id.map)
5150

5251
if (!isRestore) {
5352
map.moveCamera(
5453
CameraUpdateFactory.newLatLngZoom(
55-
LatLng(51.503186, -0.126446),
56-
10f
54+
LatLng(51.503186, -0.126446), 10f
5755
)
5856
)
5957
}
@@ -66,18 +64,13 @@ class ClusterAlgorithmsDemoActivity : BaseDemoActivity() {
6664
private fun setupSpinner() {
6765
val spinner: Spinner = findViewById(R.id.algorithm_spinner)
6866
val adapter = ArrayAdapter.createFromResource(
69-
this,
70-
R.array.clustering_algorithms,
71-
android.R.layout.simple_spinner_item
67+
this, R.array.clustering_algorithms, android.R.layout.simple_spinner_item
7268
)
7369
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
7470
spinner.adapter = adapter
7571
spinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
7672
override fun onItemSelected(
77-
parent: AdapterView<*>?,
78-
view: View?,
79-
position: Int,
80-
id: Long
73+
parent: AdapterView<*>?, view: View?, position: Int, id: Long
8174
) {
8275
setupClusterer(position)
8376
}
@@ -94,59 +87,40 @@ class ClusterAlgorithmsDemoActivity : BaseDemoActivity() {
9487
private fun setupClusterer(algorithmPosition: Int) {
9588
// 1. Clear the map and previous cluster manager
9689
map.clear()
97-
clusterManager = null
9890

9991
// 2. Initialize a new ClusterManager, using getMap() from BaseDemoActivity
10092
clusterManager = ClusterManager(this, map)
10193

10294
// 3. Set the desired algorithm based on the spinner position
103-
when (algorithmPosition) {
104-
1 -> {
105-
clusterManager?.algorithm = GridBasedAlgorithm()
106-
}
107-
108-
2 -> {
109-
clusterManager?.algorithm = NonHierarchicalDistanceBasedAlgorithm()
110-
}
111-
112-
3 -> {
113-
clusterManager?.algorithm = CentroidNonHierarchicalDistanceBasedAlgorithm()
114-
}
115-
116-
4 -> {
117-
clusterManager?.algorithm = NonHierarchicalViewBasedAlgorithm(
118-
mapView.width, mapView.height
119-
)
120-
}
121-
122-
5 -> {
123-
clusterManager?.algorithm = ContinuousZoomEuclideanCentroidAlgorithm()
124-
}
125-
126-
else -> { // Default
127-
128-
}
95+
clusterManager?.algorithm = when (algorithmPosition) {
96+
1 -> GridBasedAlgorithm()
97+
2 -> NonHierarchicalDistanceBasedAlgorithm()
98+
3 -> CentroidNonHierarchicalDistanceBasedAlgorithm()
99+
4 -> NonHierarchicalViewBasedAlgorithm(mapView.width, mapView.height)
100+
5 -> ContinuousZoomEuclideanCentroidAlgorithm()
101+
else -> error("Unsupported algorithm position: $algorithmPosition")
129102
}
130103

131104
// 4. Point the map's listeners to the ClusterManager
132105
map.setOnCameraIdleListener(clusterManager)
133106
map.setOnMarkerClickListener(clusterManager)
134107

135-
// 5. Add cluster items to the manager
136-
addItems()
108+
// 5. Generate and add cluster items to the manager
109+
val items = generateItems()
110+
clusterManager?.addItems(items)
137111

138112
// 6. Trigger the initial clustering
139113
clusterManager?.cluster()
140114
}
141115

142-
private fun addItems() {
116+
private fun generateItems(): List<MyItem> {
143117
val items = mutableListOf<MyItem>()
144118
// Add 100 random items in the map region
145-
for (i in 0..99) {
119+
for (i in 0 until 100) {
146120
val lat = 51.5145 + (Random.nextDouble() - 0.5) / 2.0
147121
val lng = -0.1245 + (Random.nextDouble() - 0.5) / 2.0
148122
items.add(MyItem(lat, lng, "Marker #$i", "Snippet for marker #$i"))
149123
}
150-
clusterManager?.addItems(items)
124+
return items
151125
}
152126
}

demo/src/main/res/layout/activity_cluster_algorithms_demo.xml

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,15 @@
1919
android:layout_width="match_parent"
2020
android:layout_height="match_parent">
2121

22-
<com.google.android.gms.maps.MapView
22+
<!-- Map Fragment -->
23+
<androidx.fragment.app.FragmentContainerView xmlns:android="http://schemas.android.com/apk/res/android"
24+
xmlns:map="http://schemas.android.com/apk/res-auto"
2325
android:id="@+id/map"
26+
android:name="com.google.android.gms.maps.SupportMapFragment"
2427
android:layout_width="match_parent"
25-
android:layout_height="match_parent" />
28+
android:layout_height="match_parent"
29+
map:mapId="mapId" />
30+
2631

2732
<Spinner
2833
android:id="@+id/algorithm_spinner"

0 commit comments

Comments
 (0)