Joeri Verlooy
Joeri Verlooy

Reputation: 613

Google maps custom marker icon with clustering on Android

I have implemented the code of Google Maps Clustering

This is the code in my activity

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();
}

This is the StoreItem Class

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;
    }
}

Now, I would like to to change the marker icons via the url parameter in the StoreItem class.

How can I do this?

Upvotes: 9

Views: 9422

Answers (3)

David Murray
David Murray

Reputation: 11

Neelesh's answer worked for me. Remember to link that renderer to your cluster

private ClusterManager<AntenaModel> clusterManager;

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

Upvotes: 1

Alexander Mironov
Alexander Mironov

Reputation: 3102

Consider overriding onBeforeClusterRendered. Something like will work:

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

Keep in mind that code is just for sample. You have to add text to bitmap manually and add some caching mechanism for real use. You can see source code of DefaultClusterRenderer for sample.

Upvotes: 5

Neelesh Atale
Neelesh Atale

Reputation: 536

you need to @override method onBeforeClusterItemRendered() Of DefaultClusterRenderer class get iconurl in onBeforeClusterItemRendered set to markerOptions please find below code:

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()));
        }
    }

Upvotes: 3

Related Questions