Search code examples
androidgoogle-mapsgoogle-maps-android-api-2google-maps-api-2

Extract value from Cluster<PersonMyStuff> in google Maps


I am trying the cluster library given by Google on my Map. The clustering is happening properly but I am trying to extract the value from cluster. I am not sure how to go about doing it?

The cluster size is 2. I am always getting the first value.

@Override
public boolean onClusterClick(Cluster<PersonMyStuff> cluster) 
{
    String firstName = cluster.getItems().iterator().next().name;

    Log.i("MyMaps","Cluster Size" +cluster.getSize());
    Log.i("MyMaps","First Name" +firstName);

    return true;
}

Let me know!

Thanks!


Solution

  • It looks like you are only accessing the first element by calling next on the iterator once. Try looping through all the elements in the collect.

    @Override
    public boolean onClusterClick(Cluster<PersonMyStuff> cluster) 
    {
        Collection<PersonMyStuff> items = cluster.getItems();
        Log.i("MyMaps","Cluster Size" +cluster.getSize());
        for(PersonMyStuff item : items) {
            Log.i("MyMaps","First Name" + item.name);
        }
    
        return true;
    }