Google会在Android上使用群集映射自定义标记图标

时间:2016-07-18 15:00:11

标签: android google-maps google-maps-markers android-maps-utils

我已经实现了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参数更改标记图标。

我该怎么做?

2 个答案:

答案 0 :(得分:5)

考虑覆盖onBeforeClusterRendered。类似的东西会起作用:

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

请记住,代码仅用于示例。您必须手动将文本添加到位图并添加一些实际使用的缓存机制。您可以查看样本的DefaultClusterRenderer源代码。

答案 1 :(得分:3)

你需要@override方法onBeforeClusterItemRendered()DefaultClusterRenderer类 将onBeforeClusterItemRendered中的iconurl设置为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()));
        }
    }