Google地图标记由缩放时的边界矩形代替 [英] Google map marker is replaced by bounding rectangle on zoom

查看:110
本文介绍了Google地图标记由缩放时的边界矩形代替的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Fragment中使用SupportMapFragment,最近使用



PS p>

  compile'c​​om.google.android.gms:play-services-maps:8.4.0'


解决方案

我使用简化版@ bishop87的解决方法来自github项目的问题。还为集群位图添加了缓存,这使得OOM更安全。



如果您没有使用集群渲染器,或将此代码移动到您自己的:
$ b SimpleClusterRenderer.java

 公共类SimpleClusterRenderer扩展DefaultClusterRenderer< AuctionItem> {
private static final int CLUSTER_PADDING = 12;
private static final int ITEM_PADDING = 7;

private final Bitmap mIconItemGreen;
private final IconGenerator mIconClusterGenerator;
私人最终浮动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
保护无效onBeforeClusterItemRendered(AuctionItem item,MarkerOptions markerOptions){
markerOptions.icon(BitmapDescriptorFactory.fromBitmap(mIconItemGreen));

$ b $覆盖
保护void onBeforeClusterRendered(Cluster< AuctionItem>集群,MarkerOptions markerOptions){
int clusterSize = getBucket(cluster);

mIconClusterGenerator.setBackground(makeClusterBackground(getColor(clusterSize)));
BitmapDescriptor描述符= BitmapDescriptorFactory.fromBitmap(mIconClusterGenerator.makeIcon(getClusterText(clusterSize)));
markerOptions.icon(描述符);
}

@Override
protected boolean shouldRenderAsCluster(Cluster< AuctionItem>集群){
//总是渲染集群。
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);
返回背景;


私有SquareTextView makeSquareTextView(上下文上下文,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);
返回squareTextView;
}
}

CachedIconGenerator.java

  public class CachedIconGenerator扩展IconGenerator {

private final LruCache< String,Bitmap> mBitmapsCache;
private String mText;

public CachedIconGenerator(Context context){
super(context);

final int maxMemory =(int)(Runtime.getRuntime()。maxMemory()/ 1024);

//使用此内存缓存的1/8可用内存。
final int cacheSize = maxMemory / 8;
mBitmapsCache = new LruCache< String,Bitmap>(cacheSize){
@Override
protected int sizeOf(String key,Bitmap bitmap){
//将测量缓存大小以千字节为单位而不是
//项目数量。
返回bitmap.getByteCount()/ 1024;
}
};
}

公共位图makeIcon(String text){
mText = text;
返回super.makeIcon(文本);

$ b @Override
public Bitmap makeIcon(){
if(TextUtils.isEmpty(mText)){
return super.makeIcon();
} else {
位图位图= mBitmapsCache.get(mText);
if(bitmap == null){
bitmap = super.makeIcon();
mBitmapsCache.put(mText,bitmap);
}
返回位图;
}
}
}

您还需要用您想要的针颜色替换 R.color.simple_green



P.P.S。忘了提及,这种方法对性能的影响可以忽略不计。因此,如果Google将在下一个Play服务应用发行版中解决此问题,则最好使用Play Services 9.0.83和其他方法更新此解决方案。


I'm using SupportMapFragment inside Fragment and recent Android Map Utils for clustering. After Google Play Services update to 9.0.83 google single map markers are replaced by bounding rectangle on zoom. Only single markers are replaced, cluster markers are fine. Changing hardware acceleration parameter in app manifest doesn't change anything. How to fix it?

P.S.

compile 'com.google.android.gms:play-services-maps:8.4.0'

解决方案

I use simplified version of @bishop87's workaround from issue on github project. Also added caching for cluster bitmaps, which made it much more OOM-safer.

If you don't have Cluster renderer than use this one or move this code to your own:

SimpleClusterRenderer.java

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

P.S. You also need to replace R.color.simple_green with pin colour you want.

P.P.S. Forgot to mention, that this approach has negligible performance impact. So it would be better to update this solution with different approaches for Play Services 9.0.83 and others, if Google will fix this issue on next Play Services app release.

这篇关于Google地图标记由缩放时的边界矩形代替的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆