-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy pathDefaultClusterRenderer.java
1179 lines (1056 loc) · 48 KB
/
DefaultClusterRenderer.java
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
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright 2023 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.view;
import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.TimeInterpolator;
import android.animation.ValueAnimator;
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.LayerDrawable;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.OvalShape;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.os.MessageQueue;
import android.util.SparseArray;
import android.view.ViewGroup;
import android.view.animation.DecelerateInterpolator;
import androidx.annotation.NonNull;
import androidx.annotation.StyleRes;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.Projection;
import com.google.android.gms.maps.model.BitmapDescriptor;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.LatLngBounds;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.maps.android.R;
import com.google.maps.android.clustering.Cluster;
import com.google.maps.android.clustering.ClusterItem;
import com.google.maps.android.clustering.ClusterManager;
import com.google.maps.android.collections.MarkerManager;
import com.google.maps.android.geometry.Point;
import com.google.maps.android.projection.SphericalMercatorProjection;
import com.google.maps.android.ui.IconGenerator;
import com.google.maps.android.ui.SquareTextView;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Queue;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
/**
* The default view for a ClusterManager. Markers are animated in and out of clusters.
*/
public class DefaultClusterRenderer<T extends ClusterItem> implements ClusterRenderer<T> {
private final GoogleMap mMap;
private final IconGenerator mIconGenerator;
private final ClusterManager<T> mClusterManager;
private final float mDensity;
private boolean mAnimate;
private long mAnimationDurationMs;
private final Executor mExecutor = Executors.newSingleThreadExecutor();
private static final int[] BUCKETS = {10, 20, 50, 100, 200, 500, 1000};
private ShapeDrawable mColoredCircleBackground;
/**
* Markers that are currently on the map.
*/
private Set<MarkerWithPosition> mMarkers = Collections.newSetFromMap(
new ConcurrentHashMap<MarkerWithPosition, Boolean>());
/**
* Icons for each bucket.
*/
private SparseArray<BitmapDescriptor> mIcons = new SparseArray<>();
/**
* Markers for single ClusterItems.
*/
private MarkerCache<T> mMarkerCache = new MarkerCache<>();
/**
* If cluster size is less than this size, display individual markers.
*/
private int mMinClusterSize = 4;
/**
* The currently displayed set of clusters.
*/
private Set<? extends Cluster<T>> mClusters;
/**
* Markers for Clusters.
*/
private MarkerCache<Cluster<T>> mClusterMarkerCache = new MarkerCache<>();
/**
* The target zoom level for the current set of clusters.
*/
private float mZoom;
private final ViewModifier mViewModifier = new ViewModifier();
private ClusterManager.OnClusterClickListener<T> mClickListener;
private ClusterManager.OnClusterInfoWindowClickListener<T> mInfoWindowClickListener;
private ClusterManager.OnClusterInfoWindowLongClickListener<T> mInfoWindowLongClickListener;
private ClusterManager.OnClusterItemClickListener<T> mItemClickListener;
private ClusterManager.OnClusterItemInfoWindowClickListener<T> mItemInfoWindowClickListener;
private ClusterManager.OnClusterItemInfoWindowLongClickListener<T> mItemInfoWindowLongClickListener;
public DefaultClusterRenderer(Context context, GoogleMap map, ClusterManager<T> clusterManager) {
mMap = map;
mAnimate = true;
mAnimationDurationMs = 300;
mDensity = context.getResources().getDisplayMetrics().density;
mIconGenerator = new IconGenerator(context);
mIconGenerator.setContentView(makeSquareTextView(context));
mIconGenerator.setTextAppearance(R.style.amu_ClusterIcon_TextAppearance);
mIconGenerator.setBackground(makeClusterBackground());
mClusterManager = clusterManager;
}
@Override
public void onAdd() {
mClusterManager.getMarkerCollection().setOnMarkerClickListener(marker -> mItemClickListener != null && mItemClickListener.onClusterItemClick(mMarkerCache.get(marker)));
mClusterManager.getMarkerCollection().setOnInfoWindowClickListener(marker -> {
if (mItemInfoWindowClickListener != null) {
mItemInfoWindowClickListener.onClusterItemInfoWindowClick(mMarkerCache.get(marker));
}
});
mClusterManager.getMarkerCollection().setOnInfoWindowLongClickListener(marker -> {
if (mItemInfoWindowLongClickListener != null) {
mItemInfoWindowLongClickListener.onClusterItemInfoWindowLongClick(mMarkerCache.get(marker));
}
});
mClusterManager.getClusterMarkerCollection().setOnMarkerClickListener(
marker -> mClickListener != null && mClickListener.onClusterClick(mClusterMarkerCache.get(marker)));
mClusterManager.getClusterMarkerCollection().setOnInfoWindowClickListener(marker -> {
if (mInfoWindowClickListener != null) {
mInfoWindowClickListener.onClusterInfoWindowClick(mClusterMarkerCache.get(marker));
}
});
mClusterManager.getClusterMarkerCollection().setOnInfoWindowLongClickListener(marker -> {
if (mInfoWindowLongClickListener != null) {
mInfoWindowLongClickListener.onClusterInfoWindowLongClick(mClusterMarkerCache.get(marker));
}
});
}
@Override
public void onRemove() {
mClusterManager.getMarkerCollection().setOnMarkerClickListener(null);
mClusterManager.getMarkerCollection().setOnInfoWindowClickListener(null);
mClusterManager.getMarkerCollection().setOnInfoWindowLongClickListener(null);
mClusterManager.getClusterMarkerCollection().setOnMarkerClickListener(null);
mClusterManager.getClusterMarkerCollection().setOnInfoWindowClickListener(null);
mClusterManager.getClusterMarkerCollection().setOnInfoWindowLongClickListener(null);
}
private LayerDrawable makeClusterBackground() {
mColoredCircleBackground = new ShapeDrawable(new OvalShape());
ShapeDrawable outline = new ShapeDrawable(new OvalShape());
outline.getPaint().setColor(0x80ffffff); // Transparent white.
LayerDrawable background = new LayerDrawable(new Drawable[]{outline, mColoredCircleBackground});
int strokeWidth = (int) (mDensity * 3);
background.setLayerInset(1, strokeWidth, strokeWidth, strokeWidth, strokeWidth);
return background;
}
private SquareTextView makeSquareTextView(Context context) {
SquareTextView squareTextView = new SquareTextView(context);
ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
squareTextView.setLayoutParams(layoutParams);
squareTextView.setId(R.id.amu_text);
int twelveDpi = (int) (12 * mDensity);
squareTextView.setPadding(twelveDpi, twelveDpi, twelveDpi, twelveDpi);
return squareTextView;
}
@Override
public int getColor(int clusterSize) {
final float hueRange = 220;
final float sizeRange = 300;
final float size = Math.min(clusterSize, sizeRange);
final float hue = (sizeRange - size) * (sizeRange - size) / (sizeRange * sizeRange) * hueRange;
return Color.HSVToColor(new float[]{
hue, 1f, .6f
});
}
@StyleRes
@Override
public int getClusterTextAppearance(int clusterSize) {
return R.style.amu_ClusterIcon_TextAppearance; // Default value
}
@NonNull
protected String getClusterText(int bucket) {
if (bucket < BUCKETS[0]) {
return String.valueOf(bucket);
}
return bucket + "+";
}
/**
* Gets the "bucket" for a particular cluster. By default, uses the number of points within the
* cluster, bucketed to some set points.
*/
protected int getBucket(@NonNull Cluster<T> cluster) {
int size = cluster.getSize();
if (size <= BUCKETS[0]) {
return size;
}
for (int i = 0; i < BUCKETS.length - 1; i++) {
if (size < BUCKETS[i + 1]) {
return BUCKETS[i];
}
}
return BUCKETS[BUCKETS.length - 1];
}
/**
* Gets the minimum cluster size used to render clusters. For example, if "4" is returned,
* then for any clusters of size 3 or less the items will be rendered as individual markers
* instead of as a single cluster marker.
*
* @return the minimum cluster size used to render clusters. For example, if "4" is returned,
* then for any clusters of size 3 or less the items will be rendered as individual markers
* instead of as a single cluster marker.
*/
public int getMinClusterSize() {
return mMinClusterSize;
}
/**
* Sets the minimum cluster size used to render clusters. For example, if "4" is provided,
* then for any clusters of size 3 or less the items will be rendered as individual markers
* instead of as a single cluster marker.
*
* @param minClusterSize the minimum cluster size used to render clusters. For example, if "4"
* is provided, then for any clusters of size 3 or less the items will be
* rendered as individual markers instead of as a single cluster marker.
*/
public void setMinClusterSize(int minClusterSize) {
mMinClusterSize = minClusterSize;
}
/**
* ViewModifier ensures only one re-rendering of the view occurs at a time, and schedules
* re-rendering, which is performed by the RenderTask.
*/
@SuppressLint("HandlerLeak")
private class ViewModifier extends Handler {
private static final int RUN_TASK = 0;
private static final int TASK_FINISHED = 1;
private boolean mViewModificationInProgress = false;
private RenderTask mNextClusters = null;
@Override
public void handleMessage(Message msg) {
if (msg.what == TASK_FINISHED) {
mViewModificationInProgress = false;
if (mNextClusters != null) {
// Run the task that was queued up.
sendEmptyMessage(RUN_TASK);
}
return;
}
removeMessages(RUN_TASK);
if (mViewModificationInProgress) {
// Busy - wait for the callback.
return;
}
if (mNextClusters == null) {
// Nothing to do.
return;
}
Projection projection = mMap.getProjection();
RenderTask renderTask;
synchronized (this) {
renderTask = mNextClusters;
mNextClusters = null;
mViewModificationInProgress = true;
}
renderTask.setCallback(() -> sendEmptyMessage(TASK_FINISHED));
renderTask.setProjection(projection);
renderTask.setMapZoom(mMap.getCameraPosition().zoom);
mExecutor.execute(renderTask);
}
public void queue(Set<? extends Cluster<T>> clusters) {
synchronized (this) {
// Overwrite any pending cluster tasks - we don't care about intermediate states.
mNextClusters = new RenderTask(clusters);
}
sendEmptyMessage(RUN_TASK);
}
}
/**
* Determine whether the cluster should be rendered as individual markers or a cluster.
*
* @param cluster cluster to examine for rendering
* @return true if the provided cluster should be rendered as a single marker on the map, false
* if the items within this cluster should be rendered as individual markers instead.
*/
protected boolean shouldRenderAsCluster(@NonNull Cluster<T> cluster) {
return cluster.getSize() >= mMinClusterSize;
}
/**
* Determines if the new clusters should be rendered on the map, given the old clusters. This
* method is primarily for optimization of performance, and the default implementation simply
* checks if the new clusters are equal to the old clusters, and if so, it returns false.
* <p>
* However, there are cases where you may want to re-render the clusters even if they didn't
* change. For example, if you want a cluster with one item to render as a cluster above
* a certain zoom level and as a marker below a certain zoom level (even if the contents of the
* clusters themselves did not change). In this case, you could check the zoom level in an
* implementation of this method and if that zoom level threshold is crossed return true, else
* {@code return super.shouldRender(oldClusters, newClusters)}.
* <p>
* Note that always returning true from this method could potentially have negative performance
* implications as clusters will be re-rendered on each pass even if they don't change.
*
* @param oldClusters The clusters from the previous iteration of the clustering algorithm
* @param newClusters The clusters from the current iteration of the clustering algorithm
* @return true if the new clusters should be rendered on the map, and false if they should not. This
* method is primarily for optimization of performance, and the default implementation simply
* checks if the new clusters are equal to the old clusters, and if so, it returns false.
*/
protected boolean shouldRender(@NonNull Set<? extends Cluster<T>> oldClusters, @NonNull Set<? extends Cluster<T>> newClusters) {
return !newClusters.equals(oldClusters);
}
/**
* Transforms the current view (represented by DefaultClusterRenderer.mClusters and DefaultClusterRenderer.mZoom) to a
* new zoom level and set of clusters.
* <p/>
* This must be run off the UI thread. Work is coordinated in the RenderTask, then queued up to
* be executed by a MarkerModifier.
* <p/>
* There are three stages for the render:
* <p/>
* 1. Markers are added to the map
* <p/>
* 2. Markers are animated to their final position
* <p/>
* 3. Any old markers are removed from the map
* <p/>
* When zooming in, markers are animated out from the nearest existing cluster. When zooming
* out, existing clusters are animated to the nearest new cluster.
*/
private class RenderTask implements Runnable {
final Set<? extends Cluster<T>> clusters;
private Runnable mCallback;
private Projection mProjection;
private SphericalMercatorProjection mSphericalMercatorProjection;
private float mMapZoom;
private RenderTask(Set<? extends Cluster<T>> clusters) {
this.clusters = clusters;
}
/**
* A callback to be run when all work has been completed.
*
* @param callback
*/
public void setCallback(Runnable callback) {
mCallback = callback;
}
public void setProjection(Projection projection) {
this.mProjection = projection;
}
public void setMapZoom(float zoom) {
this.mMapZoom = zoom;
this.mSphericalMercatorProjection = new SphericalMercatorProjection(256 * Math.pow(2, Math.min(zoom, mZoom)));
}
@SuppressLint("NewApi")
public void run() {
if (!shouldRender(immutableOf(DefaultClusterRenderer.this.mClusters), immutableOf(clusters))) {
mCallback.run();
return;
}
final MarkerModifier markerModifier = new MarkerModifier();
final float zoom = mMapZoom;
final boolean zoomingIn = zoom > mZoom;
final float zoomDelta = zoom - mZoom;
final Set<MarkerWithPosition> markersToRemove = mMarkers;
// Prevent crashes: https://issuetracker.google.com/issues/35827242
LatLngBounds visibleBounds;
try {
visibleBounds = mProjection.getVisibleRegion().latLngBounds;
} catch (Exception e) {
e.printStackTrace();
visibleBounds = LatLngBounds.builder()
.include(new LatLng(0, 0))
.build();
}
// TODO: Add some padding, so that markers can animate in from off-screen.
// Find all of the existing clusters that are on-screen. These are candidates for
// markers to animate from.
List<Point> existingClustersOnScreen = null;
if (DefaultClusterRenderer.this.mClusters != null && mAnimate) {
existingClustersOnScreen = new ArrayList<>();
for (Cluster<T> c : DefaultClusterRenderer.this.mClusters) {
if (shouldRenderAsCluster(c) && visibleBounds.contains(c.getPosition())) {
Point point = mSphericalMercatorProjection.toPoint(c.getPosition());
existingClustersOnScreen.add(point);
}
}
}
// Create the new markers and animate them to their new positions.
final Set<MarkerWithPosition> newMarkers = Collections.newSetFromMap(
new ConcurrentHashMap<MarkerWithPosition, Boolean>());
for (Cluster<T> c : clusters) {
boolean onScreen = visibleBounds.contains(c.getPosition());
if (zoomingIn && onScreen && mAnimate) {
Point point = mSphericalMercatorProjection.toPoint(c.getPosition());
Point closest = findClosestCluster(existingClustersOnScreen, point);
if (closest != null) {
LatLng animateTo = mSphericalMercatorProjection.toLatLng(closest);
markerModifier.add(true, new CreateMarkerTask(c, newMarkers, animateTo));
} else {
markerModifier.add(true, new CreateMarkerTask(c, newMarkers, null));
}
} else {
markerModifier.add(onScreen, new CreateMarkerTask(c, newMarkers, null));
}
}
// Wait for all markers to be added.
markerModifier.waitUntilFree();
// Don't remove any markers that were just added. This is basically anything that had
// a hit in the MarkerCache.
markersToRemove.removeAll(newMarkers);
// Find all of the new clusters that were added on-screen. These are candidates for
// markers to animate from.
List<Point> newClustersOnScreen = null;
if (mAnimate) {
newClustersOnScreen = new ArrayList<>();
for (Cluster<T> c : clusters) {
if (shouldRenderAsCluster(c) && visibleBounds.contains(c.getPosition())) {
Point p = mSphericalMercatorProjection.toPoint(c.getPosition());
newClustersOnScreen.add(p);
}
}
}
// Remove the old markers, animating them into clusters if zooming out.
for (final MarkerWithPosition marker : markersToRemove) {
boolean onScreen = visibleBounds.contains(marker.position);
// Don't animate when zooming out more than 3 zoom levels.
// TODO: drop animation based on speed of device & number of markers to animate.
if (!zoomingIn && zoomDelta > -3 && onScreen && mAnimate) {
final Point point = mSphericalMercatorProjection.toPoint(marker.position);
final Point closest = findClosestCluster(newClustersOnScreen, point);
if (closest != null) {
LatLng animateTo = mSphericalMercatorProjection.toLatLng(closest);
markerModifier.animateThenRemove(marker, marker.position, animateTo);
} else {
markerModifier.remove(true, marker.marker);
}
} else {
markerModifier.remove(onScreen, marker.marker);
}
}
markerModifier.waitUntilFree();
mMarkers = newMarkers;
DefaultClusterRenderer.this.mClusters = clusters;
mZoom = zoom;
mCallback.run();
}
}
@Override
public void onClustersChanged(Set<? extends Cluster<T>> clusters) {
mViewModifier.queue(clusters);
}
@Override
public void setOnClusterClickListener(ClusterManager.OnClusterClickListener<T> listener) {
mClickListener = listener;
}
@Override
public void setOnClusterInfoWindowClickListener(ClusterManager.OnClusterInfoWindowClickListener<T> listener) {
mInfoWindowClickListener = listener;
}
@Override
public void setOnClusterInfoWindowLongClickListener(ClusterManager.OnClusterInfoWindowLongClickListener<T> listener) {
mInfoWindowLongClickListener = listener;
}
@Override
public void setOnClusterItemClickListener(ClusterManager.OnClusterItemClickListener<T> listener) {
mItemClickListener = listener;
}
@Override
public void setOnClusterItemInfoWindowClickListener(ClusterManager.OnClusterItemInfoWindowClickListener<T> listener) {
mItemInfoWindowClickListener = listener;
}
@Override
public void setOnClusterItemInfoWindowLongClickListener(ClusterManager.OnClusterItemInfoWindowLongClickListener<T> listener) {
mItemInfoWindowLongClickListener = listener;
}
@Override
public void setAnimation(boolean animate) {
mAnimate = animate;
}
/**
* {@inheritDoc} The default duration is 300 milliseconds.
*
* @param animationDurationMs long: The length of the animation, in milliseconds. This value cannot be negative.
*/
@Override
public void setAnimationDuration(long animationDurationMs) {
mAnimationDurationMs = animationDurationMs;
}
private Set<? extends Cluster<T>> immutableOf(Set<? extends Cluster<T>> clusters) {
return clusters != null ? Collections.unmodifiableSet(clusters) : Collections.emptySet();
}
private static double distanceSquared(Point a, Point b) {
return (a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y);
}
private Point findClosestCluster(List<Point> markers, Point point) {
if (markers == null || markers.isEmpty()) return null;
int maxDistance = mClusterManager.getAlgorithm().getMaxDistanceBetweenClusteredItems();
double minDistSquared = maxDistance * maxDistance;
Point closest = null;
for (Point candidate : markers) {
double dist = distanceSquared(candidate, point);
if (dist < minDistSquared) {
closest = candidate;
minDistSquared = dist;
}
}
return closest;
}
/**
* Handles all markerWithPosition manipulations on the map. Work (such as adding, removing, or
* animating a markerWithPosition) is performed while trying not to block the rest of the app's
* UI.
*/
@SuppressLint("HandlerLeak")
private class MarkerModifier extends Handler implements MessageQueue.IdleHandler {
private static final int BLANK = 0;
private final Lock lock = new ReentrantLock();
private final Condition busyCondition = lock.newCondition();
private Queue<CreateMarkerTask> mCreateMarkerTasks = new LinkedList<>();
private Queue<CreateMarkerTask> mOnScreenCreateMarkerTasks = new LinkedList<>();
private Queue<Marker> mRemoveMarkerTasks = new LinkedList<>();
private Queue<Marker> mOnScreenRemoveMarkerTasks = new LinkedList<>();
private Queue<AnimationTask> mAnimationTasks = new LinkedList<>();
/**
* Whether the idle listener has been added to the UI thread's MessageQueue.
*/
private boolean mListenerAdded;
private MarkerModifier() {
super(Looper.getMainLooper());
}
/**
* Creates markers for a cluster some time in the future.
*
* @param priority whether this operation should have priority.
*/
public void add(boolean priority, CreateMarkerTask c) {
lock.lock();
sendEmptyMessage(BLANK);
if (priority) {
mOnScreenCreateMarkerTasks.add(c);
} else {
mCreateMarkerTasks.add(c);
}
lock.unlock();
}
/**
* Removes a markerWithPosition some time in the future.
*
* @param priority whether this operation should have priority.
* @param m the markerWithPosition to remove.
*/
public void remove(boolean priority, Marker m) {
lock.lock();
sendEmptyMessage(BLANK);
if (priority) {
mOnScreenRemoveMarkerTasks.add(m);
} else {
mRemoveMarkerTasks.add(m);
}
lock.unlock();
}
/**
* Animates a markerWithPosition some time in the future.
*
* @param marker the markerWithPosition to animate.
* @param from the position to animate from.
* @param to the position to animate to.
*/
public void animate(MarkerWithPosition marker, LatLng from, LatLng to) {
lock.lock();
mAnimationTasks.add(new AnimationTask(marker, from, to));
lock.unlock();
}
/**
* Animates a markerWithPosition some time in the future, and removes it when the animation
* is complete.
*
* @param marker the markerWithPosition to animate.
* @param from the position to animate from.
* @param to the position to animate to.
*/
public void animateThenRemove(MarkerWithPosition marker, LatLng from, LatLng to) {
lock.lock();
AnimationTask animationTask = new AnimationTask(marker, from, to);
animationTask.removeOnAnimationComplete(mClusterManager.getMarkerManager());
mAnimationTasks.add(animationTask);
lock.unlock();
}
@Override
public void handleMessage(Message msg) {
if (!mListenerAdded) {
Looper.myQueue().addIdleHandler(this);
mListenerAdded = true;
}
removeMessages(BLANK);
lock.lock();
try {
// Perform up to 10 tasks at once.
// Consider only performing 10 remove tasks, not adds and animations.
// Removes are relatively slow and are much better when batched.
for (int i = 0; i < 10; i++) {
performNextTask();
}
if (!isBusy()) {
mListenerAdded = false;
Looper.myQueue().removeIdleHandler(this);
// Signal any other threads that are waiting.
busyCondition.signalAll();
} else {
// Sometimes the idle queue may not be called - schedule up some work regardless
// of whether the UI thread is busy or not.
// TODO: try to remove this.
sendEmptyMessageDelayed(BLANK, 10);
}
} finally {
lock.unlock();
}
}
/**
* Perform the next task. Prioritise any on-screen work.
*/
private void performNextTask() {
if (!mOnScreenRemoveMarkerTasks.isEmpty()) {
removeMarker(mOnScreenRemoveMarkerTasks.poll());
} else if (!mAnimationTasks.isEmpty()) {
mAnimationTasks.poll().perform();
} else if (!mOnScreenCreateMarkerTasks.isEmpty()) {
mOnScreenCreateMarkerTasks.poll().perform(this);
} else if (!mCreateMarkerTasks.isEmpty()) {
mCreateMarkerTasks.poll().perform(this);
} else if (!mRemoveMarkerTasks.isEmpty()) {
removeMarker(mRemoveMarkerTasks.poll());
}
}
private void removeMarker(Marker m) {
mMarkerCache.remove(m);
mClusterMarkerCache.remove(m);
mClusterManager.getMarkerManager().remove(m);
}
/**
* @return true if there is still work to be processed.
*/
public boolean isBusy() {
try {
lock.lock();
return !(mCreateMarkerTasks.isEmpty() && mOnScreenCreateMarkerTasks.isEmpty() &&
mOnScreenRemoveMarkerTasks.isEmpty() && mRemoveMarkerTasks.isEmpty() &&
mAnimationTasks.isEmpty()
);
} finally {
lock.unlock();
}
}
/**
* Blocks the calling thread until all work has been processed.
*/
public void waitUntilFree() {
while (isBusy()) {
// Sometimes the idle queue may not be called - schedule up some work regardless
// of whether the UI thread is busy or not.
// TODO: try to remove this.
sendEmptyMessage(BLANK);
lock.lock();
try {
if (isBusy()) {
busyCondition.await();
}
} catch (InterruptedException e) {
throw new RuntimeException(e);
} finally {
lock.unlock();
}
}
}
@Override
public boolean queueIdle() {
// When the UI is not busy, schedule some work.
sendEmptyMessage(BLANK);
return true;
}
}
/**
* A cache of markers representing individual ClusterItems.
*/
private static class MarkerCache<T> {
private Map<T, Marker> mCache = new HashMap<>();
private Map<Marker, T> mCacheReverse = new HashMap<>();
public Marker get(T item) {
return mCache.get(item);
}
public T get(Marker m) {
return mCacheReverse.get(m);
}
public void put(T item, Marker m) {
mCache.put(item, m);
mCacheReverse.put(m, item);
}
public void remove(Marker m) {
T item = mCacheReverse.get(m);
mCacheReverse.remove(m);
mCache.remove(item);
}
}
/**
* Called before the marker for a ClusterItem is added to the map. The default implementation
* sets the marker and snippet text based on the respective item text if they are both
* available, otherwise it will set the title if available, and if not it will set the marker
* title to the item snippet text if that is available.
* <p>
* The first time {@link ClusterManager#cluster()} is invoked on a set of items
* {@link #onBeforeClusterItemRendered(ClusterItem, MarkerOptions)} will be called and
* {@link #onClusterItemUpdated(ClusterItem, Marker)} will not be called.
* If an item is removed and re-added (or updated) and {@link ClusterManager#cluster()} is
* invoked again, then {@link #onClusterItemUpdated(ClusterItem, Marker)} will be called and
* {@link #onBeforeClusterItemRendered(ClusterItem, MarkerOptions)} will not be called.
*
* @param item item to be rendered
* @param markerOptions the markerOptions representing the provided item
*/
protected void onBeforeClusterItemRendered(@NonNull T item, @NonNull MarkerOptions markerOptions) {
if (item.getTitle() != null && item.getSnippet() != null) {
markerOptions.title(item.getTitle());
markerOptions.snippet(item.getSnippet());
} else if (item.getTitle() != null) {
markerOptions.title(item.getTitle());
} else if (item.getSnippet() != null) {
markerOptions.title(item.getSnippet());
}
}
/**
* Called when a cached marker for a ClusterItem already exists on the map so the marker may
* be updated to the latest item values. Default implementation updates the title and snippet
* of the marker if they have changed and refreshes the info window of the marker if it is open.
* Note that the contents of the item may not have changed since the cached marker was created -
* implementations of this method are responsible for checking if something changed (if that
* matters to the implementation).
* <p>
* The first time {@link ClusterManager#cluster()} is invoked on a set of items
* {@link #onBeforeClusterItemRendered(ClusterItem, MarkerOptions)} will be called and
* {@link #onClusterItemUpdated(ClusterItem, Marker)} will not be called.
* If an item is removed and re-added (or updated) and {@link ClusterManager#cluster()} is
* invoked again, then {@link #onClusterItemUpdated(ClusterItem, Marker)} will be called and
* {@link #onBeforeClusterItemRendered(ClusterItem, MarkerOptions)} will not be called.
*
* @param item item being updated
* @param marker cached marker that contains a potentially previous state of the item.
*/
protected void onClusterItemUpdated(@NonNull T item, @NonNull Marker marker) {
boolean changed = false;
// Update marker text if the item text changed - same logic as adding marker in CreateMarkerTask.perform()
if (item.getTitle() != null && item.getSnippet() != null) {
if (!item.getTitle().equals(marker.getTitle())) {
marker.setTitle(item.getTitle());
changed = true;
}
if (!item.getSnippet().equals(marker.getSnippet())) {
marker.setSnippet(item.getSnippet());
changed = true;
}
} else if (item.getSnippet() != null && !item.getSnippet().equals(marker.getTitle())) {
marker.setTitle(item.getSnippet());
changed = true;
} else if (item.getTitle() != null && !item.getTitle().equals(marker.getTitle())) {
marker.setTitle(item.getTitle());
changed = true;
}
// Update marker position if the item changed position
if (!marker.getPosition().equals(item.getPosition())) {
marker.setPosition(item.getPosition());
if (item.getZIndex() != null) {
marker.setZIndex(item.getZIndex());
}
changed = true;
}
if (changed && marker.isInfoWindowShown()) {
// Force a refresh of marker info window contents
marker.showInfoWindow();
}
}
/**
* Called before the marker for a Cluster is added to the map.
* The default implementation draws a circle with a rough count of the number of items.
* <p>
* The first time {@link ClusterManager#cluster()} is invoked on a set of items
* {@link #onBeforeClusterRendered(Cluster, MarkerOptions)} will be called and
* {@link #onClusterUpdated(Cluster, Marker)} will not be called. If an item is removed and
* re-added (or updated) and {@link ClusterManager#cluster()} is invoked
* again, then {@link #onClusterUpdated(Cluster, Marker)} will be called and
* {@link #onBeforeClusterRendered(Cluster, MarkerOptions)} will not be called.
*
* @param cluster cluster to be rendered
* @param markerOptions markerOptions representing the provided cluster
*/
protected void onBeforeClusterRendered(@NonNull Cluster<T> cluster, @NonNull MarkerOptions markerOptions) {
// TODO: consider adding anchor(.5, .5) (Individual markers will overlap more often)
markerOptions.icon(getDescriptorForCluster(cluster));
}
/**
* Gets a BitmapDescriptor for the given cluster that contains a rough count of the number of
* items. Used to set the cluster marker icon in the default implementations of
* {@link #onBeforeClusterRendered(Cluster, MarkerOptions)} and
* {@link #onClusterUpdated(Cluster, Marker)}.
*
* @param cluster cluster to get BitmapDescriptor for
* @return a BitmapDescriptor for the marker icon for the given cluster that contains a rough
* count of the number of items.
*/
@NonNull
protected BitmapDescriptor getDescriptorForCluster(@NonNull Cluster<T> cluster) {
int bucket = getBucket(cluster);
BitmapDescriptor descriptor = mIcons.get(bucket);
if (descriptor == null) {
mColoredCircleBackground.getPaint().setColor(getColor(bucket));
mIconGenerator.setTextAppearance(getClusterTextAppearance(bucket));
descriptor = BitmapDescriptorFactory.fromBitmap(mIconGenerator.makeIcon(getClusterText(bucket)));
mIcons.put(bucket, descriptor);
}
return descriptor;
}
/**
* Called after the marker for a Cluster has been added to the map.
*
* @param cluster the cluster that was just added to the map
* @param marker the marker representing the cluster that was just added to the map
*/
protected void onClusterRendered(@NonNull Cluster<T> cluster, @NonNull Marker marker) {
}
/**
* Called when a cached marker for a Cluster already exists on the map so the marker may
* be updated to the latest cluster values. Default implementation updated the icon with a
* circle with a rough count of the number of items. Note that the contents of the cluster may
* not have changed since the cached marker was created - implementations of this method are
* responsible for checking if something changed (if that matters to the implementation).
* <p>
* The first time {@link ClusterManager#cluster()} is invoked on a set of items
* {@link #onBeforeClusterRendered(Cluster, MarkerOptions)} will be called and
* {@link #onClusterUpdated(Cluster, Marker)} will not be called. If an item is removed and
* re-added (or updated) and {@link ClusterManager#cluster()} is invoked
* again, then {@link #onClusterUpdated(Cluster, Marker)} will be called and
* {@link #onBeforeClusterRendered(Cluster, MarkerOptions)} will not be called.
*
* @param cluster cluster being updated
* @param marker cached marker that contains a potentially previous state of the cluster
*/
protected void onClusterUpdated(@NonNull Cluster<T> cluster, @NonNull Marker marker) {
// TODO: consider adding anchor(.5, .5) (Individual markers will overlap more often)
marker.setIcon(getDescriptorForCluster(cluster));
}
/**
* Called after the marker for a ClusterItem has been added to the map.
*
* @param clusterItem the item that was just added to the map
* @param marker the marker representing the item that was just added to the map
*/
protected void onClusterItemRendered(@NonNull T clusterItem, @NonNull Marker marker) {
}
/**
* Get the marker from a ClusterItem
*
* @param clusterItem ClusterItem which you will obtain its marker
* @return a marker from a ClusterItem or null if it does not exists
*/
public Marker getMarker(T clusterItem) {
return mMarkerCache.get(clusterItem);
}
/**
* Get the ClusterItem from a marker
*
* @param marker which you will obtain its ClusterItem
* @return a ClusterItem from a marker or null if it does not exists
*/
public T getClusterItem(Marker marker) {
return mMarkerCache.get(marker);
}
/**
* Get the marker from a Cluster
*
* @param cluster which you will obtain its marker
* @return a marker from a cluster or null if it does not exists
*/
public Marker getMarker(Cluster<T> cluster) {