【问题标题】:Grouping cluster Google map API分组集群 Google 地图 API
【发布时间】:2014-10-01 23:14:22
【问题描述】:

我正在将这个库用于我的一个项目 (https://github.com/googlemaps/android-maps-utils)

这个库让我可以在谷歌地图上创建集群,但我想知道是否可以按组对我的标记进行集群。例如,我想只对“朋友”的标记进行聚类,而对其他只是“同事”等的标记进行聚类……(也许不是最好的例子,但我希望你能理解)

我的想法是使用多个 ClusterManager,但我没有尝试过,也不知道它是否是最好的解决方案,甚至是一个好的解决方案。

【问题讨论】:

    标签: android google-maps-markers google-maps-api-2


    【解决方案1】:

    我找到了解决问题的方法。如果要创建多个组,更好的解决方案是管理多个 Clustermanager。

    顺便说一句,答案的所有功劳都归于 Github 上的@Brody:

    这里是链接:https://github.com/googlemaps/android-maps-utils/issues/100#event-153755438

    【讨论】:

      【解决方案2】:

      使用多个ClusterManager 很麻烦。我认为将多个 Algorithm 与包装器一起使用会更容易。

      包装器应根据项目属性选择正确的算法。唯一的要求是所有项目都必须有一个共同的父类(在下面的示例中为Item)。

      public class MultiAlgorithm<T extends ClusterItem> implements Algorithm<T> {
      
          private final Algorithm<T> friendsAlgorithm;
          private final Algorithm<T> coworkerAlgorithm;
      
          public MultiAlgorithm() {
              friendsAlgorithm = new NonHierarchicalDistanceBasedAlgorithm<>();
              coworkerAlgorithm = new NonHierarchicalDistanceBasedAlgorithm<>();
          }
      
          private Algorithm<T> getAlgorithm(T item) {
              // TODO Return the correct algorithm based on 'item' properties
          }
      
          @Override
          public void addItem(T item) {
              getAlgorithm(item).addItem(item);
          }
      
          @Override
          public void addItems(Collection<T> collection) {
              for (T item : collection) {
                  getAlgorithm(item).addItem(item);
              }
          }
      
          @Override
          public void clearItems() {
              friendsAlgorithm.clearItems();
              coworkerAlgorithm.clearItems();
          }
      
          @Override
          public void removeItem(T item) {
              getAlgorithm(item).removeItem(item);
          }
      
          @SuppressWarnings("unchecked")
          @Override
          public Set<? extends Cluster<T>> getClusters(double zoom) {
              // Use a non-typed Set to prevent some generic issue on the result.addAll() method
              Set result = new HashSet<>(friendsAlgorithm.getClusters(zoom));
              result.addAll(coworkerAlgorithm.getClusters(zoom));
              return result;
          }
      
          @Override
          public Collection<T> getItems() {
              Collection<T> result = new ArrayList<>(friendsAlgorithm.);
              result.addAll(coworkerAlgorithm.getItems());
              return result;
          }
      }
      

      用法:clusterManager.setAlgorithm(new MultiAlgorithm&lt;Item&gt;());

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-05-23
        • 2016-04-04
        • 2018-04-16
        • 2019-12-04
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多