-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expand file tree
/
Copy pathDefaultClusterItemsDistributor.java
More file actions
72 lines (55 loc) · 2.54 KB
/
DefaultClusterItemsDistributor.java
File metadata and controls
72 lines (55 loc) · 2.54 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
package com.google.maps.android.clustering.view;
import com.google.maps.android.clustering.Cluster;
import com.google.maps.android.clustering.ClusterItem;
import com.google.maps.android.clustering.ClusterManager;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* The default distributor of items included in a cluster. It distributes the items around the original lat/lng in a given radius.
*
* @param <T> Cluster item type.
*/
public class DefaultClusterItemsDistributor<T extends ClusterItem> implements ClusterItemsDistributor<T> {
private static final double DEFAULT_RADIUS = 0.00003;
private static final String DEFAULT_DELETE_LIST = "itemsDeleted";
private static final String DEFAULT_ADDED_LIST = "itemsAdded";
private double mDistributionRadius;
private ClusterManager<T> mClusterManager;
private Map<String, List<T>> mItemsCache;
public DefaultClusterItemsDistributor(ClusterManager<T> clusterManager) {
this(clusterManager, DEFAULT_RADIUS);
}
public DefaultClusterItemsDistributor(ClusterManager<T> clusterManager, double distributionRadius) {
mClusterManager = clusterManager;
mDistributionRadius = distributionRadius;
mItemsCache = new HashMap<>();
mItemsCache.put(DEFAULT_ADDED_LIST, new ArrayList<T>());
mItemsCache.put(DEFAULT_DELETE_LIST, new ArrayList<T>());
}
@Override
public void distribute(Cluster<T> cluster) {
// relocate the markers around the original markers position
int counter = 0;
float rotateFactor = (360 / cluster.getItems().size());
for (T item : cluster.getItems()) {
double lat = item.getPosition().latitude + (mDistributionRadius * Math.cos(++counter * rotateFactor));
double lng = item.getPosition().longitude + (mDistributionRadius * Math.sin(counter * rotateFactor));
T copy = (T) item.copy(lat, lng);
mClusterManager.removeItem(item);
mClusterManager.addItem(copy);
mClusterManager.cluster();
mItemsCache.get(DEFAULT_ADDED_LIST).add(copy);
mItemsCache.get(DEFAULT_DELETE_LIST).add(item);
}
}
public void collect() {
// collect the items
mClusterManager.removeItems(mItemsCache.get(DEFAULT_ADDED_LIST));
mClusterManager.addItems(mItemsCache.get(DEFAULT_DELETE_LIST));
mClusterManager.cluster();
mItemsCache.get(DEFAULT_ADDED_LIST).clear();
mItemsCache.get(DEFAULT_DELETE_LIST).clear();
}
}