Android googlemaps群集如何排除一个标记

时间:2016-05-16 12:42:54

标签: android google-maps markerclusterer

有没有办法从群集中排除一个选定的标记?

这是我的渲染器类

private class StoreRenderer extends DefaultClusterRenderer<Store> {
    HashMap<Store, Marker> storeMarkerMap = new HashMap<Store, Marker>();
    public StoreRenderer(Context context, GoogleMap map, ClusterManager<Store> clusterManager) {
        super(context, map, clusterManager);
    }

    public Marker getMarkerFromStore(Store clusterItem) {
        return storeMarkerMap.get(clusterItem);
    }

    public void removeMarkerFromStore(Store itemToRemove) {
        storeMarkerMap.remove(itemToRemove);
    }


    @Override
    protected void onBeforeClusterItemRendered(Store item, MarkerOptions markerOptions) {
        super.onBeforeClusterItemRendered(item, markerOptions);
        markerOptions.icon(BitmapDescriptorFactory.fromResource(item.getMainCategoryResourceImage()));
    }

    @Override
    protected void onClusterItemRendered(Store clusterItem, Marker marker) {
        super.onClusterItemRendered(clusterItem, marker);
        storeMarkerMap.put(clusterItem, marker);
    }
    @Override
    protected boolean shouldRenderAsCluster(Cluster cluster) {
        return cluster.getSize() > 3;
    }

    public HashMap<Store, Marker> getStoreMarkerMap() {
        return storeMarkerMap;
    }
}

我正在添加像这样的所有商店

ClusterManager<Store> mClusterManager = new ClusterManager<Store>(getContext(), map);
StoreRenderer renderer = new StoreRenderer(getContext(), my_map, mClusterManager);
mClusterManager.setRenderer(renderer);
.....
mClusterManager.addItem(temp);

mClusterManager.setOnClusterItemClickListener(new ClusterManager.OnClusterItemClickListener<Store>() {
        @Override
        public boolean onClusterItemClick(Store storeClusterItem) {
            markerClicked(storeClusterItem);
            return true;
        }
    });

如何从群集中排除点击的标记?

2 个答案:

答案 0 :(得分:0)

关于删除群集中的项目可能有很多解决方法。

  • 使用 GridBasedAlgorithm ,因为它支持元素删除。

在此SO question中,社区提供了一些解决方法,如GridBasedAlgorithm和一些用于删除群集中标记的code implementation

我希望这清楚一些事情。 :)

答案 1 :(得分:0)

这是我使用的解决方案。

clusterManager = ClusterManager(requireContext(), myMap, object : MarkerManager(myMap) {
            override fun onMarkerClick(marker: Marker): Boolean {
                val result = super.onMarkerClick(marker)
                return if (result) true // cluster marker clicked
                else {
                    //other marker click handled
                    true
                }
            }
        })
    
        clusterManager?.let { clsManager ->
            clsManager.setOnClusterItemClickListener { marker ->
                //marker in cluster clicked
                return@setOnClusterItemClickListener true //super.onMarkerClick(marker) returned true
            }   
            myMap.setOnMarkerClickListener(clsManager)
        }