Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Please give us more control on markers clustering #204

Open
2ndGAB opened this issue Jan 31, 2016 · 3 comments
Open

Please give us more control on markers clustering #204

2ndGAB opened this issue Jan 31, 2016 · 3 comments

Comments

@2ndGAB
Copy link

2ndGAB commented Jan 31, 2016

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.

@MKergall
Copy link
Owner

MKergall commented Feb 2, 2016

Really interesting.
You can subclass RadiusMarkerClusterer, to implement this feature in a custom "clusterer" method.
Or propose a pull request of RadiusMarkerClusterer.

@2ndGAB
Copy link
Author

2ndGAB commented Feb 3, 2016

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()

@2ndGAB
Copy link
Author

2ndGAB commented Jan 24, 2017

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants