Search code examples
androidgoogle-mapsgoogle-maps-api-3android-maps-v2android-maps-utils

Android Get the markers that are not clustered


I'm developing an android application and I'm using Google Maps Android API Utility Library.

To be more specific I'm using the cluster part of that library. Now to my question:

I'm interested in the markers that are shown on the map but that are currently not clustered. I've tried different stuff but can't wrap my head around it. The thing that kind of works is to save all the rendered marker items in a arraylist then compare the marker positions to LatLng bounds of the map on screen, if the markers are inside the screen bounds then the markers info will be added in a different arraylist to later be shown in a listview. The problem with that is when I zoom out, the markers are still in the renderedPersonItems array and will show up in the listview even if those markers are now clustered. I can not clear renderedPersonItems array cause the markers only render once in a lifecycle. So I'm looking for a different solution. This is what I got so far:

Code in DefaultClusterRenderer:

@Override
protected void onBeforeClusterItemRendered(PersonItem personItem, MarkerOptions markerOptions){
    markerOptions.title(personItem.personName);

    if(!renderedPersonItems.contains(personItem)){
        renderedPersonItems.add(personItem);
    }
}

Code in main activity:

LatLngBounds personOnScreenBounds = googleMap.getProjection().getVisibleRegion().latLngBounds;

for(PersonItem person : clusterPersonRenderer.getRenderedPersonItems()){
   if(PersonsOnScreenBounds.contains(person.getPosition())){
     personMarksOnScreen.add(getPersonObjectWithId(person.personId));
   }
}

Thanks in advance!


Solution

  • I can recommend you another cool android-map library. Android Maps Extensions https://github.com/mg6maciej/android-maps-extensions

    I use it in my app and it looks fine. It could be not easy to reimplement it in your app, but you could try.

    To solve your problem you can make something like this:

    List<Marker> markers = mMap.getDisplayedMarkers();
    for (Marker marker : markers) {
        if(!marker.isCluster()) {
            //
        }
    }