如何在java google地图中更改群集对象的图标

时间:2017-01-03 12:39:22

标签: java google-maps android-studio icons markerclusterer

我在Android工作室的谷歌地图的群集库中遇到了一个小问题。 问题是我只有集群对象的默认图标,如these icons

我想给标记添加一个自定义图标,但我无法在互联网上找到任何关于它的信息。我发现library page上的内容太清楚了。

提前致谢!

2 个答案:

答案 0 :(得分:0)

我找到了解决方案!这是:

@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));

}

这对我有用。

答案 1 :(得分:-1)

您可以使用位图并为群集绘制自定义视图
在您的活动中:

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

例如,您可以为群集绘制饼图
这是一个代码:

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

以上是上述代码的来源:
Google Maps Android 3D Pie Chart Marker Clustering Java