2016-05-13 19 views
13

Tôi đang sử dụng SupportMapFragment bên trong Fragment và gần đây Android Map Utils để phân cụm. Sau khi cập nhật Dịch vụ của Google Play lên 9.0.83 các điểm đánh dấu bản đồ google duy nhất được thay thế bằng hình chữ nhật bị giới hạn khi thu phóng. Chỉ có các điểm đánh dấu duy nhất được thay thế, các điểm đánh dấu cụm là tốt. Thay đổi thông số tăng tốc phần cứng trong tệp kê khai ứng dụng không thay đổi bất cứ điều gì. Làm thế nào để sửa chữa nó?Điểm đánh dấu bản đồ Google được thay thế bằng hình chữ nhật bị giới hạn trên thu phóng

enter image description here

P.S.

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

nó xuất hiện nộp cho ai đó một báo cáo lỗi về là kiện https://github.com/googlemaps/android-maps-utils/issues/276 – tyczj

Trả lời

5

Theo tyczj, điều này bắt đầu xảy ra khi dịch vụ Google Play (chương trình độc quyền) cập nhật lên 9.0.x.

Từ vẻ của các cuộc thảo luận vấn đề Github, thực hiện giải pháp là:

thể workarround từ trang gmaps-api-vấn đề:

thay đổi marker.setIcon (BitmapDescriptorFactory.fromResource (R .drawable.drawableid));

đến marker.setIcon (BitmapDescriptorFactory.fromBitmap (BitmapFactory.decodeResource (getResources(), R.drawable.drawableid))); // có thể ảnh hưởng đến mức tiêu thụ bộ nhớ chung tho (?), tôi chưa thử nghiệm nhiều hơn ứng dụng được báo cáo trong số phát hành .

Cũng chú ý:

Tôi có thể khẳng định vấn đề này. Ngoài @Mavamaarten, bạn không được sử dụng lại hình ảnh điểm đánh dấu.

(Nguồn: https://github.com/googlemaps/android-maps-utils/issues/276)

+0

Điều đó có nghĩa là gì. "bạn không được sử dụng lại hình ảnh đánh dấu" ?? –

+1

Về cơ bản, giải pháp thay thế là bạn luôn tải các bản sao bitmap mới ... thay vì chia sẻ cùng một bản sao. –

+0

Tôi đang chia sẻ cùng một và tôi không nhận được lỗi. Điều này có bình thường không? –

7

tôi sử dụng phiên bản đơn giản của @ bishop87 của workaround from issue on github project. Cũng đã thêm bộ nhớ đệm cho các bitmap cụm, điều này làm cho OOM an toàn hơn nhiều.

Nếu bạn không có cụm renderer vì sử dụng cái này hoặc di chuyển mã này vào của riêng bạn:

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

PS Bạn cũng cần thay thế R.color.simple_green bằng màu pin bạn muốn.

P.P.S. Quên đề cập đến, cách tiếp cận này có tác động hiệu suất không đáng kể. Vì vậy, tốt hơn nên cập nhật giải pháp này với các cách tiếp cận khác nhau cho Dịch vụ của Play 9.0.83 và các giải pháp khác, nếu Google sẽ khắc phục sự cố này trên bản phát hành ứng dụng Dịch vụ Play tiếp theo.

+1

Tạo BitmapDescriptor riêng cho mỗi mục có thể gây ra OutOfMemoryException hoặc tương tự trên một số thiết bị. Theo tôi hiểu, nó bởi vì cụm thư viện cho thấy tất cả các điểm trước khi tính toán lại một phiên bản nhóm. – tse

+0

@tse, Thật không may, bạn đã đúng. Nhưng tôi đã cập nhật dự án của tôi với bộ nhớ cache, và bây giờ nó là cách an toàn hơn. Tôi cũng sẽ cập nhật câu trả lời này hôm nay. – Oleksandr

+0

Có, giải pháp hoạt động - nhưng tạo ra rất nhiều OutOfMemoryException (s). – LaLiLuLeLo

0

Đối với tôi sự cố xảy ra khi: (1) Tôi xóa một điểm đánh dấu bằng biểu tượng tùy chỉnh hoặc (2) đặt biểu tượng mới sau khi tạo ...

Để giải quyết trường hợp đầu tiên, bạn cần phải thiết lập các biểu tượng mặc định trước khi loại bỏ ...

if (marker != null) { 
    marker.setIcon(BitmapDescriptorFactory.defaultMarker()); 
    marker.remove(); 
} 

Để giải quyết trường hợp thứ hai, bạn cần phải thêm một bản sao của các điểm đánh dấu với biểu tượng tùy chỉnh mới và sau đó loại bỏ trong trường hợp đầu tiên cùng ...

Cho đến Google Maps Đội khắc phục vấn đề này, nó là một giải pháp ...

Chúc may mắn ...

Các vấn đề liên quan