Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Refreshing makers (ClusterItems) in Google Maps v2 for Android

I'm using Google Maps Android API Utility Library and I'm downloading certain images from internet that I want to use as markers. The way I'm doing it is like in the following snippet:

class MarkerItemClusterRenderer extends DefaultClusterRenderer<MarkerItem> {
...
    @Override
        protected void onBeforeClusterItemRendered(MarkerItem item,
                final MarkerOptions markerOptions) {
            super.onBeforeClusterItemRendered(item, markerOptions);
            mImageLoader.get(item.getImageUrl(), new ImageListener() {

                @Override
                public void onErrorResponse(VolleyError error) {
                    Log.i("XXX", error.toString());
                }

                @Override
                public void onResponse(ImageContainer response, boolean isImmediate) {
                    if (response != null && response.getBitmap() != null) {
                        mImageIcon.setImageBitmap(response.getBitmap());
                        Bitmap icon = mIconGenerator.makeIcon();
                        Bitmap bhalfsize = Bitmap.createScaledBitmap(icon, 150,
                                150, false);
                        markerOptions.icon(BitmapDescriptorFactory
                                .fromBitmap(bhalfsize));
                    }
                }
            });
        }

The problem is, that when the image is downloaded, the map (and thus the marker) doesn't refresh, so most of the times (but not always) I still see the red default markers.

I tried to do mImageIcon.invalidate(); mImageIcon.requestLayout(); but there's still no luck.

Is there anyway to achieve this? Thanks a lot in advance.

like image 359
noloman Avatar asked Mar 24 '14 13:03

noloman


1 Answers

You just need to make all this stuff in

protected void onClusterItemRendered(T clusterItem, Marker marker) {
    ...
}

In onBeforeClusterItemRendered you set icon on MarkerOptions in async callback. At this time it could be added to map and become real Marker. So you icon will be set to already useless object.

That's why you need to do it in onClusterItemRendered

like image 89
Stas Parshin Avatar answered Oct 14 '22 22:10

Stas Parshin