[英]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.