-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expand file tree
/
Copy pathClusterManager.java
More file actions
503 lines (441 loc) · 18.3 KB
/
ClusterManager.java
File metadata and controls
503 lines (441 loc) · 18.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
/*
* Copyright 2013 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.maps.android.clustering;
import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.model.CameraPosition;
import com.google.android.gms.maps.model.Marker;
import com.google.maps.android.clustering.algo.Algorithm;
import com.google.maps.android.clustering.algo.NonHierarchicalDistanceBasedAlgorithm;
import com.google.maps.android.clustering.algo.PreCachingAlgorithmDecorator;
import com.google.maps.android.clustering.algo.ScreenBasedAlgorithm;
import com.google.maps.android.clustering.algo.ScreenBasedAlgorithmAdapter;
import com.google.maps.android.clustering.view.ClusterRenderer;
import com.google.maps.android.clustering.view.DefaultClusterRenderer;
import com.google.maps.android.collections.MarkerManager;
import java.util.Collection;
import java.util.Set;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
/**
* Groups many items on a map based on zoom level.
* <p>
* ClusterManager should be added to the map as an: <ul> <li>{@link com.google.android.gms.maps.GoogleMap.OnCameraIdleListener}</li>
* <li>{@link com.google.android.gms.maps.GoogleMap.OnMarkerClickListener}</li> </ul>
*/
public class ClusterManager<T extends ClusterItem> implements
GoogleMap.OnCameraIdleListener,
GoogleMap.OnMarkerClickListener,
GoogleMap.OnInfoWindowClickListener {
private final MarkerManager mMarkerManager;
private final MarkerManager.Collection mMarkers;
private final MarkerManager.Collection mClusterMarkers;
private ScreenBasedAlgorithm<T> mAlgorithm;
private ClusterRenderer<T> mRenderer;
private GoogleMap mMap;
private CameraPosition mPreviousCameraPosition;
private ClusterTask mClusterTask;
private final ReadWriteLock mClusterTaskLock = new ReentrantReadWriteLock();
private OnClusterItemClickListener<T> mOnClusterItemClickListener;
private OnClusterInfoWindowClickListener<T> mOnClusterInfoWindowClickListener;
private OnClusterInfoWindowCloseListener<T> mOnClusterInfoWindowCloseListener;
private OnClusterInfoWindowLongClickListener<T> mOnClusterInfoWindowLongClickListener;
private OnClusterItemInfoWindowClickListener<T> mOnClusterItemInfoWindowClickListener;
private OnClusterItemInfoWindowCloseListener<T> mOnClusterItemInfoWindowCloseListener;
private OnClusterItemInfoWindowLongClickListener<T> mOnClusterItemInfoWindowLongClickListener;
private OnClusterClickListener<T> mOnClusterClickListener;
public ClusterManager(Context context, GoogleMap map) {
this(context, map, new MarkerManager(map));
}
public ClusterManager(Context context, GoogleMap map, MarkerManager markerManager) {
mMap = map;
mMarkerManager = markerManager;
mClusterMarkers = markerManager.newCollection();
mMarkers = markerManager.newCollection();
mRenderer = new DefaultClusterRenderer<>(context, map, this);
mAlgorithm = new ScreenBasedAlgorithmAdapter<>(new PreCachingAlgorithmDecorator<>(
new NonHierarchicalDistanceBasedAlgorithm<T>()));
mClusterTask = new ClusterTask();
mRenderer.onAdd();
}
public MarkerManager.Collection getMarkerCollection() {
return mMarkers;
}
public MarkerManager.Collection getClusterMarkerCollection() {
return mClusterMarkers;
}
public MarkerManager getMarkerManager() {
return mMarkerManager;
}
public void setRenderer(ClusterRenderer<T> renderer) {
mRenderer.setOnClusterClickListener(null);
mRenderer.setOnClusterItemClickListener(null);
mClusterMarkers.clear();
mMarkers.clear();
mRenderer.onRemove();
mRenderer = renderer;
mRenderer.onAdd();
mRenderer.setOnClusterClickListener(mOnClusterClickListener);
mRenderer.setOnClusterInfoWindowClickListener(mOnClusterInfoWindowClickListener);
mRenderer.setOnClusterInfoWindowCloseListener(mOnClusterInfoWindowCloseListener);
mRenderer.setOnClusterInfoWindowLongClickListener(mOnClusterInfoWindowLongClickListener);
mRenderer.setOnClusterItemClickListener(mOnClusterItemClickListener);
mRenderer.setOnClusterItemInfoWindowClickListener(mOnClusterItemInfoWindowClickListener);
mRenderer.setOnClusterItemInfoWindowCloseListener(mOnClusterItemInfoWindowCloseListener);
mRenderer.setOnClusterItemInfoWindowLongClickListener(mOnClusterItemInfoWindowLongClickListener);
cluster();
}
public void setAlgorithm(Algorithm<T> algorithm) {
if (algorithm instanceof ScreenBasedAlgorithm) {
setAlgorithm((ScreenBasedAlgorithm<T>) algorithm);
} else {
setAlgorithm(new ScreenBasedAlgorithmAdapter<>(algorithm));
}
}
public void setAlgorithm(ScreenBasedAlgorithm<T> algorithm) {
algorithm.lock();
try {
final Algorithm<T> oldAlgorithm = getAlgorithm();
mAlgorithm = algorithm;
if (oldAlgorithm != null) {
oldAlgorithm.lock();
try {
algorithm.addItems(oldAlgorithm.getItems());
} finally {
oldAlgorithm.unlock();
}
}
} finally {
algorithm.unlock();
}
if (mAlgorithm.shouldReclusterOnMapMovement()) {
mAlgorithm.onCameraChange(mMap.getCameraPosition());
}
cluster();
}
public void setAnimation(boolean animate) {
mRenderer.setAnimation(animate);
}
public ClusterRenderer<T> getRenderer() {
return mRenderer;
}
public Algorithm<T> getAlgorithm() {
return mAlgorithm;
}
/**
* Removes all items from the cluster manager. After calling this method you must invoke
* {@link #cluster()} for the map to be cleared.
*/
public void clearItems() {
final Algorithm<T> algorithm = getAlgorithm();
algorithm.lock();
try {
algorithm.clearItems();
} finally {
algorithm.unlock();
}
}
/**
* Adds items to clusters. After calling this method you must invoke {@link #cluster()} for the
* state of the clusters to be updated on the map.
* @param items items to add to clusters
* @return true if the cluster manager contents changed as a result of the call
*/
public boolean addItems(Collection<T> items) {
final Algorithm<T> algorithm = getAlgorithm();
algorithm.lock();
try {
return algorithm.addItems(items);
} finally {
algorithm.unlock();
}
}
/**
* Adds an item to a cluster. After calling this method you must invoke {@link #cluster()} for
* the state of the clusters to be updated on the map.
* @param myItem item to add to clusters
* @return true if the cluster manager contents changed as a result of the call
*/
public boolean addItem(T myItem) {
final Algorithm<T> algorithm = getAlgorithm();
algorithm.lock();
try {
return algorithm.addItem(myItem);
} finally {
algorithm.unlock();
}
}
public void diff(@Nullable Collection<T> add, @Nullable Collection<T> remove, @Nullable Collection<T> modify) {
final Algorithm<T> algorithm = getAlgorithm();
algorithm.lock();
try {
// Add items
if (add != null) {
for (T item : add) {
algorithm.addItem(item);
}
}
// Remove items
if (remove != null) {
algorithm.removeItems(remove);
}
// Modify items
if (modify != null) {
for (T item : modify) {
updateItem(item);
}
}
} finally {
algorithm.unlock();
}
}
/**
* Removes items from clusters. After calling this method you must invoke {@link #cluster()} for
* the state of the clusters to be updated on the map.
* @param items items to remove from clusters
* @return true if the cluster manager contents changed as a result of the call
*/
public boolean removeItems(Collection<T> items) {
final Algorithm<T> algorithm = getAlgorithm();
algorithm.lock();
try {
return algorithm.removeItems(items);
} finally {
algorithm.unlock();
}
}
/**
* Removes an item from clusters. After calling this method you must invoke {@link #cluster()}
* for the state of the clusters to be updated on the map.
* @param item item to remove from clusters
* @return true if the item was removed from the cluster manager as a result of this call
*/
public boolean removeItem(T item) {
final Algorithm<T> algorithm = getAlgorithm();
algorithm.lock();
try {
return algorithm.removeItem(item);
} finally {
algorithm.unlock();
}
}
/**
* Updates an item in clusters. After calling this method you must invoke {@link #cluster()} for
* the state of the clusters to be updated on the map.
* @param item item to update in clusters
* @return true if the item was updated in the cluster manager, false if the item is not
* contained within the cluster manager and the cluster manager contents are unchanged
*/
public boolean updateItem(T item) {
final Algorithm<T> algorithm = getAlgorithm();
algorithm.lock();
try {
return algorithm.updateItem(item);
} finally {
algorithm.unlock();
}
}
/**
* Force a re-cluster on the map. You should call this after adding, removing, updating,
* or clearing item(s).
*/
public void cluster() {
mClusterTaskLock.writeLock().lock();
try {
// Attempt to cancel the in-flight request.
mClusterTask.cancel(true);
mClusterTask = new ClusterTask();
mClusterTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, mMap.getCameraPosition().zoom);
} finally {
mClusterTaskLock.writeLock().unlock();
}
}
/**
* Might re-cluster.
*/
@Override
public void onCameraIdle() {
if (mRenderer instanceof GoogleMap.OnCameraIdleListener) {
((GoogleMap.OnCameraIdleListener) mRenderer).onCameraIdle();
}
mAlgorithm.onCameraChange(mMap.getCameraPosition());
// delegate clustering to the algorithm
if (mAlgorithm.shouldReclusterOnMapMovement()) {
cluster();
// Don't re-compute clusters if the map has just been panned/tilted/rotated.
} else if (mPreviousCameraPosition == null || mPreviousCameraPosition.zoom != mMap.getCameraPosition().zoom) {
mPreviousCameraPosition = mMap.getCameraPosition();
cluster();
}
}
@Override
public boolean onMarkerClick(@NonNull Marker marker) {
return getMarkerManager().onMarkerClick(marker);
}
@Override
public void onInfoWindowClick(@NonNull Marker marker) {
getMarkerManager().onInfoWindowClick(marker);
}
/**
* Runs the clustering algorithm in a background thread, then re-paints when results come back.
*/
private class ClusterTask extends AsyncTask<Float, Void, Set<? extends Cluster<T>>> {
@Override
protected Set<? extends Cluster<T>> doInBackground(Float... zoom) {
final Algorithm<T> algorithm = getAlgorithm();
algorithm.lock();
try {
return algorithm.getClusters(zoom[0]);
} finally {
algorithm.unlock();
}
}
@Override
protected void onPostExecute(Set<? extends Cluster<T>> clusters) {
mRenderer.onClustersChanged(clusters);
}
}
/**
* Sets a callback that's invoked when a Cluster is tapped. Note: For this listener to function,
* the ClusterManager must be added as a click listener to the map.
*/
public void setOnClusterClickListener(OnClusterClickListener<T> listener) {
mOnClusterClickListener = listener;
mRenderer.setOnClusterClickListener(listener);
}
/**
* Sets a callback that's invoked when a Cluster info window is tapped. Note: For this listener to function,
* the ClusterManager must be added as a info window click listener to the map.
*/
public void setOnClusterInfoWindowClickListener(OnClusterInfoWindowClickListener<T> listener) {
mOnClusterInfoWindowClickListener = listener;
mRenderer.setOnClusterInfoWindowClickListener(listener);
}
/**
* Sets a callback that's invoked when a Cluster info window is closed. Note: For this listener to function,
* the ClusterManager must be added as a info window click listener to the map.
*/
public void setOnClusterInfoWindowCloseListener(OnClusterInfoWindowCloseListener<T> listener) {
mOnClusterInfoWindowCloseListener = listener;
mRenderer.setOnClusterInfoWindowCloseListener(listener);
}
/**
* Sets a callback that's invoked when a Cluster info window is long-pressed. Note: For this listener to function,
* the ClusterManager must be added as a info window click listener to the map.
*/
public void setOnClusterInfoWindowLongClickListener(OnClusterInfoWindowLongClickListener<T> listener) {
mOnClusterInfoWindowLongClickListener = listener;
mRenderer.setOnClusterInfoWindowLongClickListener(listener);
}
/**
* Sets a callback that's invoked when an individual ClusterItem is tapped. Note: For this
* listener to function, the ClusterManager must be added as a click listener to the map.
*/
public void setOnClusterItemClickListener(OnClusterItemClickListener<T> listener) {
mOnClusterItemClickListener = listener;
mRenderer.setOnClusterItemClickListener(listener);
}
/**
* Sets a callback that's invoked when an individual ClusterItem's Info Window is tapped. Note: For this
* listener to function, the ClusterManager must be added as a info window click listener to the map.
*/
public void setOnClusterItemInfoWindowClickListener(OnClusterItemInfoWindowClickListener<T> listener) {
mOnClusterItemInfoWindowClickListener = listener;
mRenderer.setOnClusterItemInfoWindowClickListener(listener);
}
/**
* Sets a callback that's invoked when an individual ClusterItem's Info Window is closed. Note: For this
* listener to function, the ClusterManager must be added as a info window click listener to the map.
*/
public void setOnClusterItemInfoWindowCloseListener(OnClusterItemInfoWindowCloseListener<T> listener) {
mOnClusterItemInfoWindowCloseListener = listener;
mRenderer.setOnClusterItemInfoWindowCloseListener(listener);
}
/**
* Sets a callback that's invoked when an individual ClusterItem's Info Window is long-pressed. Note: For this
* listener to function, the ClusterManager must be added as a info window click listener to the map.
*/
public void setOnClusterItemInfoWindowLongClickListener(OnClusterItemInfoWindowLongClickListener<T> listener) {
mOnClusterItemInfoWindowLongClickListener = listener;
mRenderer.setOnClusterItemInfoWindowLongClickListener(listener);
}
/**
* Called when a Cluster is clicked.
*/
public interface OnClusterClickListener<T extends ClusterItem> {
/**
* Called when cluster is clicked.
* Return true if click has been handled
* Return false and the click will dispatched to the next listener
*/
boolean onClusterClick(Cluster<T> cluster);
}
/**
* Called when a Cluster's Info Window is clicked.
*/
public interface OnClusterInfoWindowClickListener<T extends ClusterItem> {
void onClusterInfoWindowClick(Cluster<T> cluster);
}
/**
* Called when a Cluster's Info Window is closed.
*/
public interface OnClusterInfoWindowCloseListener<T extends ClusterItem> {
void onClusterInfoWindowClose(Cluster<T> cluster);
}
/**
* Called when a Cluster's Info Window is long clicked.
*/
public interface OnClusterInfoWindowLongClickListener<T extends ClusterItem> {
void onClusterInfoWindowLongClick(Cluster<T> cluster);
}
/**
* Called when an individual ClusterItem is clicked.
*/
public interface OnClusterItemClickListener<T extends ClusterItem> {
/**
* Called when {@code item} is clicked.
*
* @param item the item clicked
*
* @return true if the listener consumed the event (i.e. the default behavior should not
* occur), false otherwise (i.e. the default behavior should occur). The default behavior
* is for the camera to move to the marker and an info window to appear.
*/
boolean onClusterItemClick(T item);
}
/**
* Called when an individual ClusterItem's Info Window is clicked.
*/
public interface OnClusterItemInfoWindowClickListener<T extends ClusterItem> {
void onClusterItemInfoWindowClick(T item);
}
/**
* Called when an individual ClusterItem's Info Window is closed.
*/
public interface OnClusterItemInfoWindowCloseListener<T extends ClusterItem> {
void onClusterItemInfoWindowClose(T item);
}
/**
* Called when an individual ClusterItem's Info Window is long clicked.
*/
public interface OnClusterItemInfoWindowLongClickListener<T extends ClusterItem> {
void onClusterItemInfoWindowLongClick(T item);
}
}