Changing the marker icon using android-maps-extensions

1.6k views Asked by At

I moved to android-maps-extensions for clustering. But I want to change the icons of the markers during runtime. In the original google maps lib markers have a setIcon method, that is missing in the extension lib. Would it be feasible to add the method to the Marker implementation or should I look for another workaround like deleting the marker and add a new one instead of changing the icon?

2

There are 2 answers

2
MaciejGórski On BEST ANSWER

Version 1.3.1 you can directly download from the project site, didn't have setIcon added.

The currect status is version 1.4 is about to be released and it includes both setIcon and setAnchor. I have some problems with uploading zip file to code.google.com.

If you don't want to use git to clone the repository, you may grab the latest version from GitHub repo: https://github.com/mg6maciej/android-maps-extensions/archive/master.zip

Note that setIcon will only work for Markers you add to the map. You will not be able to change clusters icons this way for now.

Edit:

Changing ClusterMarker.setIcon code from

@Override
public void setIcon(BitmapDescriptor icon) {
    throw new UnsupportedOperationException();
}

to something like:

@Override
public void setIcon(BitmapDescriptor icon) {
    if (virtual != null) {
        virtual.setIcon(icon);
    }
}

should work in most cases.

0
user1530779 On

If you want to achieve a custom clustering icon

Do this ..........

for (Cluster cluster : clusterList) {

    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inMutable = true;
    options.inPurgeable = true;
    Bitmap bitmap = BitmapFactory.decodeResource(getResources(),
            R.drawable.cluster_marker, options);

    Canvas canvas = new Canvas(bitmap);

    Paint paint = new Paint();
    paint.setColor(getResources().getColor(R.color.white));
    paint.setTextSize(30);

    canvas.drawText(String.valueOf(cluster.getMarkerList().size()), 10,
            40, paint);

    googleMap.addMarker(new MarkerOptions()
            .position(
                    new LatLng(cluster.getClusterLatitude(), cluster
                            .getClusterLongitude()))
            .snippet(String.valueOf(cluster.getMarkerList().size()))
            .title("Cluster")
            .icon(BitmapDescriptorFactory.fromBitmap(bitmap)));

}

where cluster marker is my drawable and I'm writing text over it.