当我有一个群集17,18时,它显示" 10 +"," 20 +"。如何显示确切的数字?
感谢。
答案 0 :(得分:5)
public class MyClusterItemRenderer extends DefaultClusterRenderer<MyClusterItem> {
private final IconGenerator mIconGenerator;
private final ShapeDrawable mColoredCircleBackground;
private final float mDensity;
private SparseArray<BitmapDescriptor> mIcons = new SparseArray();
public MyClusterItemRenderer(Context context, GoogleMap map, ClusterManager<MyClusterItem> clusterManager) {
super(context, map, clusterManager);
mIconGenerator = new IconGenerator(context);
mColoredCircleBackground = new ShapeDrawable(new OvalShape());
mDensity = context.getResources().getDisplayMetrics().density;
SquareTextView squareTextView = new SquareTextView(context);
ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(-2, -2);
squareTextView.setLayoutParams(layoutParams);
squareTextView.setId(com.google.maps.android.R.id.text);
int twelveDpi = (int)(12.0F * this.mDensity);
squareTextView.setPadding(twelveDpi, twelveDpi, twelveDpi, twelveDpi);
this.mIconGenerator.setContentView(squareTextView);
mIconGenerator.setTextAppearance(com.google.maps.android.R.style.ClusterIcon_TextAppearance);
ShapeDrawable outline = new ShapeDrawable(new OvalShape());
outline.getPaint().setColor(Color.parseColor("#FFFFFF"));
LayerDrawable background = new LayerDrawable(new Drawable[]{outline, this.mColoredCircleBackground});
int strokeWidth = (int) (this.mDensity * 3.0F);
background.setLayerInset(1, strokeWidth, strokeWidth, strokeWidth, strokeWidth);
mIconGenerator.setBackground(background);
}
@Override
protected void onBeforeClusterRendered(Cluster<MyClusterItem> cluster, MarkerOptions markerOptions) {
int bucket = this.getBucket(cluster);
BitmapDescriptor descriptor = (BitmapDescriptor)this.mIcons.get(bucket);
if(descriptor == null) {
this.mColoredCircleBackground.getPaint().setColor(this.getColor(bucket));
descriptor = BitmapDescriptorFactory.fromBitmap(this.mIconGenerator.makeIcon(String.valueOf(cluster.getSize())));
this.mIcons.put(bucket, descriptor);
}
markerOptions.icon(descriptor);
}
}
活动:
clusterManager.setRenderer(new MyClusterItemRenderer(getApplicationContext(), mGoogleMap, clusterManager));
答案 1 :(得分:3)
默认情况下,Google Map提供大约数量的集群项目。要获得准确的集群项数,只需在自定义集群渲染器类中重写以下两个方法:
@Override
protected String getClusterText(int bucket) {
return String.valueOf(bucket);
}
@Override
protected int getBucket(Cluster<MyItem> cluster) {
return cluster.getSize();
}
完整的代码段是:
public class MarkerClusterRenderer extends DefaultClusterRenderer<MyItem> {
MarkerClusterRenderer(Context context, GoogleMap map, ClusterManager<MyItem> clusterManager) {
super(context, map, clusterManager);
}
@Override
protected void onBeforeClusterItemRendered(MyItem item, MarkerOptions markerOptions) {
// use this to make your change to the marker option
// for the marker before it gets render on the map
markerOptions.icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_map_pins));
}
@Override
protected void onClusterItemRendered(MyItem item, Marker marker) {
//set marker title, tag, snippet, e.t.c here
}
@Override
protected String getClusterText(int bucket) {
return String.valueOf(bucket);
}
@Override
protected int getBucket(Cluster<MyItem> cluster) {
return cluster.getSize();
}
}
这是结果:
快乐编码:)
答案 2 :(得分:0)
您可以实现ClusterRenderer来自定义群集的呈现。 DefaultClusterRenderer提供了一个很好的基础。通过继承DefaultClusterRenderer,您可以覆盖默认值。
有关自定义的深入示例,请查看实用程序库附带的demo app中的CustomMarkerClusteringDemoActivity。
答案 3 :(得分:0)
这个有点老了,但是因为它帮助我找到了一个更简单的解决方案,所以我以为我在这里发布了。
Berdimurat的想法正确,只是解决方案更简单。
@Override
protected int getBucket(Cluster<FlagItemCluster> cluster) {
return cluster.getSize();
}
@Override
protected String getClusterText(int bucket) {
return String.valueOf(bucket);
}
基本上,而不是使用onBeforeClusterRendered,只需更改它获取数据的位置即可。此方法还纠正由于getBucket逻辑包含的内容而导致显示的数字不正确的情况。