Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to deal with Large number of ClusterItems in Cluster - Android GoogleMaps V2

I am using GoogleMapsV2. I have around 10 000 cluster items which I bulk add to ClusterManager (ClusterManager.addItems(Collection).

My problem is that the clustering and de-clusterin process is lagging and slow. I believe it shouldn't be that slow with around 10 000 items. Testing on OnePlus3 (with 6GB or ram) so it's not the phone.

Here is cluster relate code in activity:

// Rent markers
rentMarkerList = new ArrayList<>();
rentClusterManager = new ClusterManager<OffersMarker>(this, gmap);
rentClusterRenderer = new OffersClusterRenderer(this, gmap, rentClusterManager);
    rentClusterManager.setRenderer(rentClusterRenderer);

for (AdMarker adMarker : adsArray) { //<-- adsArray.size() = 6500
    OffersMarker offsetItem = makeOffersMarker(adLocation, nrOfAds, realEstateAdIds, OffersMarker.DEALTYPE.SALE_CODE);
    rentMarkerList.add(offsetItem);
}

// Do bulk add of Markers into ClusterManager.
rentClusterManager.addItems(saleMarkerList);
rentClusterManager.cluster();

I went over my ClusterRenderer and I am not performing any large operations in onBeforeClusterItemRendered() and onBeforeClusterRendered(). It seems that the application is much slower when doing Clustering than it is in de-clustering but that may also be because clustering is done usually on a much smaller zoom level and thus more markers are displayed.

My android monitor shows that the clustering process alters the memory and CPU and not the GPU so it's not a graphics problem.

Solutions I tried: Doing dynamic cluster rendering like described here. In this case you loop through your markers list and see which are in the Viewport bounds so you only display those markers. This made the whole thing worse because I had to loop through all the 10 000 markers every time onCameraIdle... was called.

Any ideas where to start optimizing this problem?

like image 543
KasparTr Avatar asked Oct 18 '25 21:10

KasparTr


1 Answers

Do you use rentMarkerList anywhere else? If not, you can refactor your code in this way:

rentClusterManager = new ClusterManager<OffersMarker>(this, gmap);
rentClusterRenderer = new OffersClusterRenderer(this, gmap, rentClusterManager);
rentClusterManager.setRenderer(rentClusterRenderer);

for (AdMarker adMarker : adsArray) { //<-- adsArray.size() = 6500
   rentClusterManager.addItem(makeOffersMarker(adLocation, nrOfAds, realEstateAdIds, OffersMarker.DEALTYPE.SALE_CODE));
}

rentClusterManager.cluster();

So you don't create an extra list of markers and don't add them all at the same time, but add them one by one.

You can also repleace foreach loop with simple for loop. According to this Android performance patterns episode, it works faster.

I don't think that that will significantly improve the performance, it's just little optimization tips, that may help.

like image 84
frozzyk Avatar answered Oct 20 '25 09:10

frozzyk