谷歌Map自定义标记图标与Android上的集群

odopli94  于 2023-06-28  发布在  Android
关注(0)|答案(3)|浏览(129)

我已经实现了Google Maps Clustering的代码
这是我活动中的代码

private void setUpClusterer() {
    mClusterManager = new ClusterManager<StoreItem>(this, mMap);
    mMap.setOnCameraChangeListener(mClusterManager);
    mMap.setOnMarkerClickListener(mClusterManager);
}

public void addItems(List<Store> stores) {
    for (Store store : stores) {
        mClusterManager.addItem(new StoreItem(store.getImage(), store.getLocation().getLatitude(), store.getLocation().getLongitude()));
    }
}

private void removeAllItems() {
    mClusterManager.clearItems();
}

这是StoreItem类

public class StoreItem implements ClusterItem {

    private String url;
    private final LatLng mPosition;

    public StoreItem(String url, double lat, double lng) {
        this.url = url;
        mPosition = new LatLng(lat, lng);
    }

    @Override
    public LatLng getPosition() {
        return mPosition;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }
}

现在,我想通过StoreItem类中的url参数来更改标记图标。
我该怎么做?

gcuhipw9

gcuhipw91#

考虑重写onBeforeClusterRendered。类似的东西会工作:

BitmapDescriptor descriptor = BitmapDescriptorFactory.fromBitmap(here_goes_your_bitmap);
markerOptions.icon(descriptor);

请记住,代码只是示例。您必须手动添加文本到位图,并添加一些缓存机制,以供真实的使用。您可以查看DefaultClusterRenderer的源代码作为示例。

ffx8fchx

ffx8fchx2#

你需要@override方法onBeforeClusterItemRendered()Of DefaultClusterRenderer class get iconurl in onBeforeClusterItemRendered set to markerOptions请找到下面的代码:

private class CustomMapClusterRenderer<T extends ClusterItem> extends DefaultClusterRenderer<T> {
        CustomMapClusterRenderer(Context context, GoogleMap map, ClusterManager<T> clusterManager) {
            super(context, map, clusterManager);
        }

        @Override
        protected boolean shouldRenderAsCluster(Cluster<T> cluster) {
            //start clustering if 2 or more items overlap
            return cluster.getSize() >= Constants.MINIMUM_CLUSTER_SIZE;
        }

        @Override
        protected void onBeforeClusterItemRendered(T item,
                                                   MarkerOptions markerOptions) {
            ClusterMarkerItem markerItem = (ClusterMarkerItem) item;
            markerOptions.icon(BitmapDescriptorFactory.fromPath(markerItem.getURL()));
        }
    }
1cklez4t

1cklez4t3#

Neelesh的回答对我很有效。记住将该渲染器链接到集群

private ClusterManager<AntenaModel> clusterManager;

clusterManager = new ClusterManager<>(ctx, mMap);
clusterManager.setRenderer(new CustomMapClusterRenderer<>(ctx, mMap, clusterManager));

相关问题