简体   繁体   English

如何在Java Google Maps中更改群集对象的图标

[英]How do I change the icons for the cluster objects in java google maps

I'm stuck with a little problem in the Clustering library for google maps in Android studio. 我在Android Studio中的Google地图群集库中遇到了一个小问题。 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 Google Maps Android 3D饼图标记群集Java

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Java Android Google Maps更改我之前所在的颜色折线 - Java android google maps change color polyline where I was 如何在Codename 1中更改Google Maps api中路径的样式 - How do I change the style of a path in the Google Maps api in Codename one 如何在Java代码中使用Google Maps API来获取指定位置的坐标? - How do I use Google Maps API in a Java code to fetch coordinates of specified location? 如何在flutter中将一个图标更改为两个不同的图标? - How do I change one icon into two different icons in flutter? 如何比较Java摆动按钮图标以检查是否相等? - How do I compare java swing button icons to check equality? 如何将序列化的Java对象从Swing应用程序发送到Google App Engine应用程序,反之亦然? - How do I send serialized java objects from a swing application to a google app engine application and vice versa? 如何根据 Java 中其他对象值的变化使 object 更新其值? - How do I make object to update its value, based on other objects value change, in Java? 如何使用 Java 代码向 Flink 集群提交作业? - How do I submit a job to a Flink cluster using Java code? 如何在Android上使用Google Maps Directions API? - How do I use Google Maps Directions API on Android? 如何在java中返回一个对象数组? - How do I return an array of objects in java?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM