Google Maps Utility:如何从ClusterManager中获取所有标记? [英] Google Maps Utility: how to get all markers from ClusterManager<?>?

查看:125
本文介绍了Google Maps Utility:如何从ClusterManager中获取所有标记?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对不起,我的英语

我尝试了ClusterManager<?>.getMarkerCollection().getMarkers()方法,但是它返回了空集合.

I tried the ClusterManager<?>.getMarkerCollection().getMarkers() method but it returns empty collection.

我在我的应用程序Google Maps Utility Library中使用.每次旋转屏幕后,我都会创建AsynkTask,并在后台线程中从DB中读取数据并将项目添加到ClusterManager:

I use in my app Google Maps Utility Library. Every time after a screen rotation I create AsynkTask and in background thread read data from DB and add items to ClusterManager:

cursor.moveToFirst();
while (!cursor.isAfterLast()) {
    SomeData row = readSomeDataRow(cursor);
    clusterManager.addItem(new ClusterItemImpl(row));
    cursor.moveToNext();
}

AsyncTask完成工作后(即在主线程中),我尝试从ClusterManager获取所有标记:

When the AsyncTask finished its work (i.e. in main thread) I tried to get all markers from the ClusterManager:

clusterManager.cluster();
// cluster manager returns empty collection  \|/
markers = clusterManager.getMarkerCollection().getMarkers(); 

,但是ClusterManager返回空集合.

可能是在我调用getMarkers()的那一刻,ClusterManager却没有在地图上放置标记,稍后会做(可能在后台线程中).如果是这样,那我该如何抓住那一刻?

May be at the moment when I call getMarkers() the ClusterManager yet doesn't place markers on map and will do it a bit later (may be in background thread). If so, then how can I catch that moment?

推荐答案

我将为您提供一个不错的解决方法.首先,我将提供一些背景知识.然后,我将告诉您修改代码的非常简单的方法.

I will give you a nice workaround. First, I will give some background. Then, I will tell you the very simple method for modifying your code.

背景:让我们首先从库代码中查看ClusterManager.addItem的实现:

BACKGROUND: Let's first look at the implementation of ClusterManager.addItem from the library code:

public void addItem(T myItem) {
    this.mAlgorithmLock.writeLock().lock();

    try {
        this.mAlgorithm.addItem(myItem);
    } finally {
        this.mAlgorithmLock.writeLock().unlock();
    }

}

如您所见,当您调用clusterManager.addItem时,ClusterManager随后将调用this.mAlgorithm.addItem. mAlgorithm是存储项目的位置.现在让我们看一下ClusterManager的默认构造函数:

As you can see, when you call clusterManager.addItem, the ClusterManager then calls this.mAlgorithm.addItem. mAlgorithm is where your item is stored. Let's now look at the default constructor of ClusterManager:

public ClusterManager(Context context, GoogleMap map, MarkerManager markerManager) {
    ...
    this.mAlgorithm = new PreCachingAlgorithmDecorator(new  NonHierarchicalDistanceBasedAlgorithm());
    ...        
}

mAlgorithm实例化为包含NonHierarchicalDistanceBasedAlgorithm的PreCachingAlgorithmDecorator.不幸的是,由于mAlgorithm被声明为私有,因此我们无权访问要添加到算法中的项.但是,很高兴有一个简单的解决方法!我们仅使用ClusterManager.setAlgorithm实例化mAlgorithm.这使我们可以访问算法类.

mAlgorithm is instantiated as a PreCachingAlgorithmDecorator containing a NonHierarchicalDistanceBasedAlgorithm. Unfortunately, since mAlgorithm is declared private, we don't have access to the items which are being added to the algorithm. However, there is happily an easy workaround! We simply instantiate mAlgorithm using ClusterManager.setAlgorithm. This allows us access to the algorithm class.

解决方法:这是插入了解决方法的代码.

WORKAROUND: Here is your code with the workaround inserted.

  1. 将此声明与您的类变量一起放置:

  1. Put this declaration with your class variables:

private Algorithm<Post> clusterManagerAlgorithm;

  • 在实例化ClusterManager的位置,之后立即放置它:

  • In the place where you instantiate your ClusterManager, put this immediately afterwards:

     // Instantiate the cluster manager algorithm as is done in the ClusterManager
     clusterManagerAlgorithm = new NonHierarchicalDistanceBasedAlgorithm();
    
     // Set this local algorithm in clusterManager
     clusterManager.setAlgorithm(clusterManagerAlgorithm);
    

  • 您可以完全相同地保留用于将项目插入集群的代码.

  • You can leave your code for inserting items into the cluster exactly the same.

    当您要访问插入的项目时,只需使用与ClusterManager相反的算法即可:

    When you want to get access to the items inserted, you simply use the algorithm as opposed to the ClusterManager:

    Collection<ClusterItemImpl> items = clusterManagerAlgorithm.getItems();
    

  • 这将返回项目而不是Marker对象,但是我相信这是您所需要的.

    This returns the items instead of the Marker objects, but I believe it is what you need.

    这篇关于Google Maps Utility:如何从ClusterManager中获取所有标记?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆