简体   繁体   English

如何处理 Cluster 中大量 ClusterItems - Android GoogleMaps V2

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

I am using GoogleMapsV2.我正在使用 GoogleMapsV2。 I have around 10 000 cluster items which I bulk add to ClusterManager (ClusterManager.addItems(Collection).我有大约 10 000 个集群项目,我将它们批量添加到 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.我相信大约 10 000 个项目不应该那么慢。 Testing on OnePlus3 (with 6GB or ram) so it's not the phone.在 OnePlus3(6GB 或内存)上进行测试,所以它不是手机。

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().我检查了我的 ClusterRenderer,我没有在 onBeforeClusterItemRendered() 和 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.我的 android 监视器显示集群过程改变了内存和 CPU,而不是 GPU,所以这不是图形问题。

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.这使整个事情变得更糟,因为每次调用 onCameraIdle... 时我都必须遍历所有 10 000 个标记。

Any ideas where to start optimizing this problem?任何想法从哪里开始优化这个问题?

Do you use rentMarkerList anywhere else?你在其他地方使用rentMarkerList吗? 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.您还可以用简单的 for 循环替换 foreach 循环。 According to this Android performance patterns episode , it works faster.根据这个Android 性能模式集,它运行得更快。

I don't think that that will significantly improve the performance, it's just little optimization tips, that may help.我认为这不会显着提高性能,这只是一些优化技巧,可能会有所帮助。

After straggling with 30K markers on ios i just dropped google cluster manager, it has very poor preformance and not scalable at all.在 ios 上使用 30K 标记后,我刚刚放弃了 google 集群管理器,它的性能很差,根本无法扩展。 there plenty other free cluster libs, just drop google-utiils.还有很多其他免费的集群库,只需删除 google-utils。

I have swapped it for ClusterKit - works grate!我已经把它换成了 ClusterKit - 很好用! https://github.com/hulab/ClusterKit - (iOS) https://github.com/hulab/ClusterKit - (iOS)

There is implementation of a view-based clustering algorithm in android-maps-utils library that can be used for better performance of clustering.android-maps-utils库中有一个基于视图的聚类算法的实现,可以用于更好的聚类性能。 I had 5000+ markers and clustering was struggling when zooming in and out.我有 5000 多个标记,并且在放大和缩小时聚类很困难。 After setting Algorithm to NonHierarchicalViewBasedAlgorithm, clustering is very smooth and faster.将 Algorithm 设置为 NonHierarchicalViewBasedAlgorithm 后,聚类非常流畅和快速。

mClusterManager = new ClusterManager<>(this.mContext, mGoogleMap);

DisplayMetrics metrics = new DisplayMetrics();
this.mActivity.getWindowManager().getDefaultDisplay().getMetrics(metrics);
mClusterManager.setAlgorithm(new NonHierarchicalViewBasedAlgorithm<MyItem>(metrics.widthPixels, metrics.heightPixels));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM