我正在Fragment中使用SupportMapFragment,并使用最近的Android Map Utils进行聚类。在Google Play服务更新到9.0.83后,谷歌单个地图标记被缩放时替换为边界矩形。只有单个标记被替换,聚类标记正常。更改应用程序清单中的硬件加速参数不会改变任何内容。如何解决?
P.S.
compile 'com.google.android.gms:play-services-maps:8.4.0'
我正在Fragment中使用SupportMapFragment,并使用最近的Android Map Utils进行聚类。在Google Play服务更新到9.0.83后,谷歌单个地图标记被缩放时替换为边界矩形。只有单个标记被替换,聚类标记正常。更改应用程序清单中的硬件加速参数不会改变任何内容。如何解决?
P.S.
compile 'com.google.android.gms:play-services-maps:8.4.0'
public class SimpleClusterRenderer extends DefaultClusterRenderer<AuctionItem> {
private static final int CLUSTER_PADDING = 12;
private static final int ITEM_PADDING = 7;
private final Bitmap mIconItemGreen;
private final IconGenerator mIconClusterGenerator;
private final float mDensity;
public SimpleClusterRenderer(Context context, GoogleMap map, ClusterManager<AuctionItem> clusterManager) {
super(context, map, clusterManager);
mDensity = context.getResources().getDisplayMetrics().density;
mIconClusterGenerator = new CachedIconGenerator(context);
mIconClusterGenerator.setContentView(makeSquareTextView(context, CLUSTER_PADDING));
mIconClusterGenerator.setTextAppearance(com.google.maps.android.R.style.ClusterIcon_TextAppearance);
IconGenerator iconItemGenerator = new IconGenerator(context);
iconItemGenerator.setContentView(makeSquareTextView(context, ITEM_PADDING));
iconItemGenerator.setBackground(makeClusterBackground(ContextCompat.getColor(context, R.color.simple_green)));
mIconItemGreen = iconItemGenerator.makeIcon();
}
@Override
protected void onBeforeClusterItemRendered(AuctionItem item, MarkerOptions markerOptions) {
markerOptions.icon(BitmapDescriptorFactory.fromBitmap(mIconItemGreen));
}
@Override
protected void onBeforeClusterRendered(Cluster<AuctionItem> cluster, MarkerOptions markerOptions) {
int clusterSize = getBucket(cluster);
mIconClusterGenerator.setBackground(makeClusterBackground(getColor(clusterSize)));
BitmapDescriptor descriptor = BitmapDescriptorFactory.fromBitmap(mIconClusterGenerator.makeIcon(getClusterText(clusterSize)));
markerOptions.icon(descriptor);
}
@Override
protected boolean shouldRenderAsCluster(Cluster<AuctionItem> cluster) {
// Always render clusters.
return cluster.getSize() > 1;
}
private int getColor(int clusterSize) {
float size = Math.min((float) clusterSize, 300.0F);
float hue = (300.0F - size) * (300.0F - size) / 90000.0F * 220.0F;
return Color.HSVToColor(new float[]{hue, 1.0F, 0.6F});
}
private LayerDrawable makeClusterBackground(int color) {
ShapeDrawable mColoredCircleBackground = new ShapeDrawable(new OvalShape());
mColoredCircleBackground.getPaint().setColor(color);
ShapeDrawable outline = new ShapeDrawable(new OvalShape());
outline.getPaint().setColor(0x80ffffff);
LayerDrawable background = new LayerDrawable(new Drawable[]{outline, mColoredCircleBackground});
int strokeWidth = (int) (mDensity * 3.0F);
background.setLayerInset(1, strokeWidth, strokeWidth, strokeWidth, strokeWidth);
return background;
}
private SquareTextView makeSquareTextView(Context context, int padding) {
SquareTextView squareTextView = new SquareTextView(context);
ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
squareTextView.setLayoutParams(layoutParams);
squareTextView.setId(R.id.text);
int paddingDpi = (int) (padding * mDensity);
squareTextView.setPadding(paddingDpi, paddingDpi, paddingDpi, paddingDpi);
return squareTextView;
}
}
CachedIconGenerator.java
public class CachedIconGenerator extends IconGenerator {
private final LruCache<String, Bitmap> mBitmapsCache;
private String mText;
public CachedIconGenerator(Context context) {
super(context);
final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);
// Use 1/8th of the available memory for this memory cache.
final int cacheSize = maxMemory / 8;
mBitmapsCache = new LruCache<String, Bitmap>(cacheSize) {
@Override
protected int sizeOf(String key, Bitmap bitmap) {
// The cache size will be measured in kilobytes rather than
// number of items.
return bitmap.getByteCount() / 1024;
}
};
}
public Bitmap makeIcon(String text) {
mText = text;
return super.makeIcon(text);
}
@Override
public Bitmap makeIcon() {
if (TextUtils.isEmpty(mText)) {
return super.makeIcon();
} else {
Bitmap bitmap = mBitmapsCache.get(mText);
if (bitmap == null) {
bitmap = super.makeIcon();
mBitmapsCache.put(mText, bitmap);
}
return bitmap;
}
}
}
附注:您还需要将 R.color.simple_green
替换为您想要的标记颜色。
另外,忘了提到,这种方法几乎不会影响性能。因此,如果谷歌在下一个Play服务应用程序版本中修复了此问题,则最好使用不同的方法更新此解决方案以适应Play Services 9.0.83和其他版本。
java.lang.RuntimeException: Could not copy bitmap to parcel blob
错误,这是由 BitmapDescriptor descriptor = BitmapDescriptorFactory.fromBitmap(mIconClusterGenerator.makeIcon(getClusterText(clusterSize)));
在 onBeforeClusterRendered
中触发的。 - Dellkanif (marker != null) {
marker.setIcon(BitmapDescriptorFactory.defaultMarker());
marker.remove();
}