简体   繁体   English

Google Maps v2 for Android中的刷新制作者(ClusterItems)

[英]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. 我正在使用谷歌地图Android API实用程序库 ,我正在从互联网上下载我想用作标记的某些图像。 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(); 我试着做mImageIcon.invalidate(); mImageIcon.requestLayout(); mImageIcon.invalidate(); mImageIcon.requestLayout(); but there's still no luck. 但仍然没有运气。

Is there anyway to achieve this? 反正有没有实现这个目标? Thanks a lot in advance. 非常感谢提前。

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. onBeforeClusterItemRendered您在异步回调中的MarkerOptions上设置图标。 At this time it could be added to map and become real Marker . 此时它可以添加到地图并成为真正的Marker So you icon will be set to already useless object. 所以你的图标将被设置为已经无用的对象。

That's why you need to do it in onClusterItemRendered 这就是你需要在onClusterItemRendered完成它的onClusterItemRendered

Let's say you have GoogleMap object declared as: private GoogleMap mMap; 假设您将GoogleMap对象声明为:private GoogleMap mMap;

In onResponse() method before applying any change to marker, try writing following statement to clear previous markers: mMap.clear(); 在对标记应用任何更改之前的onResponse()方法中,尝试编写以下语句以清除以前的标记:mMap.clear();

Now set your new marker. 现在设置新标记。

I might be a bit late but i write it down so it can be useful for somebody looking for a solution like i was. 我可能会有点迟,但我把它写下来,这对于寻找像我这样的解决方案的人来说非常有用。 Basically what you have to do is refresh the marker and not the ClusterItem , but i used my own ClusterItem implementation to store some important data. 基本上你要做的是刷新标记而不是ClusterItem ,但我使用自己的ClusterItem实现来存储一些重要的数据。 So your code inside onBeforeClusterItemRendered becomes like this: 所以你在onBeforeClusterItemRendered的代码变成这样:

LatLngBounds bounds = map.getProjection().getVisibleRegion().latLngBounds; //take visible region on map
if(bounds.contains(item.getPosition()) && !item.hasImage()) { //if item is not inside that region or it has an image already don't load his image
    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);

                    //Set has image flag
                    item.setHasImage(true);

                    //Find the right marker
                    MarkerManager.Collection markerCollection = mClusterManager.getMarkerCollection();
                    Collection<Marker> markers = markerCollection.getMarkers();
                    for (Marker m : markers) {
                        if (id.equals(m.getTitle())) {
                            //set the icon
                            m.setIcon(BitmapDescriptorFactory.fromBitmap(image));
                            break;
                        }
                    }
                }
            }
    });
}

And your MyItem class must have some parameters which are useful for remember our stuff: 而你的MyItem类必须有一些参数,这些参数对于记住我们的东西很有用:

public class MyItem implements ClusterItem {

private String itemId;
private LatLng mPosition;
private WMWall wall;
private boolean hasImage = false;

public MyItem(double latitude, double longitude) {
    mPosition = new LatLng(latitude, longitude);
}

@Override
public LatLng getPosition() {
    return mPosition;
}

public WMWall getWall() {
    return wall;
}

public void setWall(WMWall wall) {
    this.wall = wall;
}

public String getItemId() {
    return itemId;
}

public void setItemId(String itemId) {
    this.itemId = itemId;
}

public boolean hasImage() {
    return hasImage;
}

public void setHasImage(boolean hasImage) {
    this.hasImage = hasImage;
}
}

It is really important to load only the images of markers contained into bounds, otherwise you'll run into OOM. 仅加载包含在边界中的标记图像非常重要,否则您将遇到OOM。 And if the hasImage() method returns true we don't need to load the image again since it is already stored into the marker object. 如果hasImage()方法返回true,我们不需要再次加载图像,因为它已经存储在标记对象中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM