Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 438
Joeri Verlooy Avatar asked Jul 18 '16 15:07

Joeri Verlooy


People also ask

How do I start a custom image on Google Maps marker for mobile app?

You can start by looking at the Canvas and Drawables from the Android Developer page. Now you also want to download a picture from an URL. URL url = new URL(user_image_url); HttpURLConnection conn = (HttpURLConnection) url. openConnection(); conn.


1 Answers

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.

like image 136
Alexander Mironov Avatar answered Sep 17 '22 14:09

Alexander Mironov