Please give us more control on markers clustering
Hi,
A missing feature prevents to only cluster when necessary.
From my point of view, the basic goal of clustering is to prevent to have to many markers on maps and not no hide them. And currently, the problem is that if you have only 2 markers close enough on the map, they are mandatory clustered and you have to zoom to max level to explode them. That's really not convenient.
The solution proposed by googlemaps api is to define a minimum number of close markers to decide to group them together. And I think it's a quite good solution.
Really interesting. You can subclass RadiusMarkerClusterer, to implement this feature in a custom "clusterer" method. Or propose a pull request of RadiusMarkerClusterer.
Ok, so my proposal is:
public class MyRadiusMarkerClusterer extends RadiusMarkerClusterer {
public int getMinNumOfMarkersInCluster() {
return minNumOfMarkersInCluster;
}
public void setMinNumOfMarkersInCluster(int minNumOfMarkersInCluster) {
this.minNumOfMarkersInCluster = minNumOfMarkersInCluster;
}
int minNumOfMarkersInCluster = 1; // Initialized to 1 to keep the same behavior as today
public MyRadiusMarkerClusterer(Context ctx) {
super(ctx);
}
@Override public ArrayList<StaticCluster> clusterer(MapView mapView) {
ArrayList<StaticCluster> clusters = super.clusterer(mapView);
ArrayList<StaticCluster> myClusters = new ArrayList<>();
Iterator<StaticCluster> itClusters = clusters.iterator();
while(itClusters.hasNext()) {
StaticCluster cluster = itClusters.next();
if (cluster.getSize() < minNumOfMarkersInCluster) {
for (int i = 0; i < cluster.getSize(); i++) {
StaticCluster newCluster = new StaticCluster(cluster.getItem(i).getPosition());
newCluster.add(cluster.getItem(i));
myClusters.add(newCluster);
}
} else {
myClusters.add(cluster);
}
}
return myClusters;
}
}
But it could be implemented more efficiently in the createCluster()
Overloading clusterer() method causes a problem sometimes in the count of markers:
for example, I've added the following code in overloaded clusterer() the goal is to force some types of markers to not be included in a cluster:
public MyRadiusMarkerClusterer(Context ctx) {
super(ctx);
}
@Override public ArrayList<StaticCluster> clusterer(MapView mapView) {
ArrayList<StaticCluster> clusters = super.clusterer(mapView);
ArrayList<StaticCluster> myClusters = new ArrayList<>();
Iterator<StaticCluster> itClusters = clusters.iterator();
while(itClusters.hasNext()) {
StaticCluster cluster = itClusters.next();
if (cluster.getSize() < minNumOfMarkersInCluster) {
for (int i = 0; i < cluster.getSize(); i++) {
StaticCluster newCluster = new StaticCluster(cluster.getItem(i).getPosition());
newCluster.add(cluster.getItem(i));
myClusters.add(newCluster);
}
} else { // ! ! ! ! Here is were the problem occurs ! ! !
for (int i = 0; i < cluster.getSize(); i++) {
if ("ag".equals(cluster.getItem(i).getSubDescription()) || "in".equals(cluster.getItem(i).getSubDescription())) {
StaticCluster newCluster = new StaticCluster(cluster.getItem(i).getPosition());
newCluster.add(cluster.getItem(i));
myClusters.add(newCluster);
}
}
myClusters.add(cluster);
}
}
clusters.clear();
return myClusters;
}
So for some types of markers, identified by ag or in in subdescription, which is a bit annoying because visible in the infoWindow :-(, I create a new cluster. That way, it will be distinctly displayed.
But it's still counted in clusters.
So the result is if I have 5 markers close together but 2 special ones, I will display 2 markers + a cluster displaying 5 instead of 3!
do you have an idea to fix it?