@@ -24,6 +24,7 @@ import com.google.android.gms.maps.CameraUpdateFactory
2424import com.google.android.gms.maps.MapView
2525import com.google.android.gms.maps.model.LatLng
2626import com.google.maps.android.clustering.ClusterManager
27+ import com.google.maps.android.clustering.algo.AbstractAlgorithm
2728import com.google.maps.android.clustering.algo.CentroidNonHierarchicalDistanceBasedAlgorithm
2829import com.google.maps.android.clustering.algo.ContinuousZoomEuclideanCentroidAlgorithm
2930import 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}
0 commit comments