I'm stuck with a little problem in the Clustering library for google maps in Android studio. The problem is that I only have the default icons for the cluster objects, like these icons .
I would like to give the markers a custom icon, but I cant find anything about it on internet. And I find it too unclear on the library page .
Thanks in advance!
I found a solution! Here it is:
@Override
protected void onBeforeClusterRendered(Cluster<MyItem> cluster, MarkerOptions markerOptions)
{
final Drawable clusterIcon = ContextCompat.getDrawable(MainActivity.context,R.drawable.[your_icon]);
mClusterIconGenerator.setBackground(clusterIcon);
//modify padding for one or two digit numbers
if (cluster.getSize() < 10) {
mClusterIconGenerator.setContentPadding(40, 20, 0, 0);
}
else
{
mClusterIconGenerator.setContentPadding(30, 20, 0, 0);
}
Bitmap icon = mClusterIconGenerator.makeIcon(String.valueOf(cluster.getSize()));
markerOptions.icon(BitmapDescriptorFactory.fromBitmap(icon));
}
This worked for me.
You can use Bitmap and draw a custom view for your cluster
In your Activity:
@Override
protected void onBeforeClusterRendered(Cluster<Asset> cluster, MarkerOptions markerOptions) {
//Draw
values = calculateData(values);
PieChart pieChart = new PieChart(values, realValues);
mClusterImageView.setImageDrawable(pieChart);
Bitmap icon = mClusterIconGenerator.makeIcon(cluster.getSize() + "");
markerOptions.icon(BitmapDescriptorFactory.fromBitmap(icon)).anchor(.5f, .5f);
}
For example you can draw a piechart for cluster
Here is a code:
public class PieChart extends Drawable {
private Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
private float[] value_degree;
private final float[] value_real;
private int[] COLORS = {0xFF74E370, 0xFF54B8FF, 0xFFFF5754, 0xFF939393};
RectF rectf = new RectF(convertDpToPixel(20),convertDpToPixel(17),convertDpToPixel(80), convertDpToPixel(57));
RectF rect = new RectF(convertDpToPixel(20),convertDpToPixel(17),convertDpToPixel(80), convertDpToPixel(67));
int temp = 0;
public PieChart(float[] values, float[] realValues) {
value_degree = new float[]{values[0], values[1], values[2], values[3]};
value_real = new float[]{realValues[0], realValues[1], realValues[2], realValues[3]};
}
@Override
public void draw(Canvas canvas) {
paint.setColor(Color.BLACK);
paint.setShadowLayer(10.0f, 0.0f, 2.0f, 0xFF000000);
paint.setStrokeWidth(100);
paint.setColor(0xAA777777);
canvas.drawOval(rect, paint);
for (int i = 0; i < value_degree.length; i++) {
if (i == 0) {
paint.setShadowLayer(10.0f, 0.0f, 2.0f, 0xFF000000);
paint.setStrokeWidth(1);
paint.setColor(COLORS[i]);
canvas.drawArc(rectf, 0, value_degree[i], true, paint);
paint.setColor(Color.BLACK);
paint.setTextSize(convertDpToPixel(13));
if (value_real[i] != 0) {
double x = 40 * Math.cos(Math.toRadians(value_degree[i] / 2)) + 40;
double y = 35 * Math.sin(Math.toRadians(value_degree[i] / 2)) + 40;
canvas.drawText((int) value_real[i]+"", convertDpToPixel((float) x), convertDpToPixel((float) y), paint);
}
} else {
paint.setShadowLayer(10.0f, 0.0f, 2.0f, 0xFF000000);
temp += (int) value_degree[i - 1];
paint.setColor(COLORS[i]);
canvas.drawArc(rectf, temp, value_degree[i], true, paint);
paint.setColor(Color.BLACK);
paint.setTextSize(convertDpToPixel(13));
if (value_real[i] != 0) {
double x = 40 * Math.cos(Math.toRadians(temp + (value_degree[i] / 2))) +40;
double y = 35 * Math.sin(Math.toRadians(temp + (value_degree[i] / 2))) + 45;
canvas.drawText((int) value_real[i] + "", convertDpToPixel((float) x), convertDpToPixel((float) y), paint);
}
}
}
}
}
Here is a source of above code:
Google Maps Android 3D Pie Chart Marker Clustering Java
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.