Clustermanager Repaint Markers Of Google Maps V2 Utils
Solution 1:
mClusterManager.cluster();
force re-clustering items when you after added new item.
Solution 2:
Seems that I found a workaround.
ClusterManager uses a renderer, in this case it inherits from DefaultClusterRenderer which uses a internal cache, a cache of markers that are added to map. You can access directly to the added markers on the map, I don't use the info window, so i add marker options.title() an ID for later find this marker, so:
@OverrideprotectedvoidonBeforeClusterItemRendered(TweetClusterItem item, MarkerOptions markerOptions) {
.... Blabla code....
markerOptions.title(Long.toString(tweet.getId()));
.... Blabla code....
}
and when I want to reload the clusterItem I call this method:
/**
* Workarround to repaint markers
* @param item item to repaint
*/publicvoidreloadMarker(TweetClusterItem item) {
MarkerManager.CollectionmarkerCollection= clusterManager.getMarkerCollection();
Collection<Marker> markers = markerCollection.getMarkers();
StringstrId= Long.toString(item.getTweet().getId());
for (Marker m : markers) {
if (strId.equals(m.getTitle())) {
m.setIcon( ICON TO SET);
break;
}
}
}
Maybe is a little hacky but it works and I din't found any other way to do this. If you found another better way, please share :)
Solution 3:
You can get specific markers that correspond to their cluster or cluster items and vice versa in O(1) using DefaultClusterRenderer's getMarker(), getCluster() and getClusterItem() (set your own renderer to access the renderer object).
Use these methods to change the markers of your items whenever you need.
...
DefaultClusterRenderermRenderer= ...
mClusterManager.setRenderer(mRenderer);
...
publicvoidreloadMarker(ClusterItem item) {
mRenderer.getMarker(item).setIcon(YOUR_ICON);
}
I wouldn't recommend saving them anywhere else though, since those methods return the renderer's cache objects.
Solution 4:
I was having the same exact problem. None of the suggested solutions were working for me. I made a class which extends the DefaultClusterRenderer and adds the public method updateClusterItem(ClusterItem clusterItem) which will force the Marker associated with that ClusterItem to be re-rendered (works with both clusters and cluster items).
import android.content.Context;
import android.support.annotation.CallSuper;
import android.support.annotation.NonNull;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.maps.android.clustering.Cluster;
import com.google.maps.android.clustering.ClusterItem;
import com.google.maps.android.clustering.ClusterManager;
import com.google.maps.android.clustering.view.DefaultClusterRenderer;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
publicabstractclassCustomClusterRenderer<T extendsClusterItem>
extendsDefaultClusterRenderer<T> {
private ClusterManager<T> mClusterManager;
private Map<T, Marker> mClusterMap = newHashMap<>();
publicCustomClusterRenderer(Context context, GoogleMap map,
ClusterManager<T> clusterManager) {
super(context, map, clusterManager);
mClusterManager = clusterManager;
}
@Override@CallSuperprotectedvoidonClusterItemRendered(T clusterItem, Marker marker) {
super.onClusterItemRendered(clusterItem, marker);
mClusterMap.remove(clusterItem);
cleanCache();
}
@Override@CallSuperprotectedvoidonClusterRendered(Cluster<T> cluster, Marker marker) {
super.onClusterRendered(cluster, marker);
for (T clusterItem : cluster.getItems()) {
mClusterMap.put(clusterItem, marker);
}
cleanCache();
}
publicvoidupdateClusterItem(T clusterItem) {
Markermarker= getMarker(clusterItem);
booleanisCluster=false;
if (marker == null) {
marker = mClusterMap.get(clusterItem);
isCluster = marker != null;
}
if (marker != null) {
MarkerOptionsoptions= getMarkerOptionsFromMarker(marker);
if (isCluster) {
Clustercluster= getCluster(marker);
onBeforeClusterRendered(cluster, options);
} else {
onBeforeClusterItemRendered(clusterItem, options);
}
loadMarkerWithMarkerOptions(marker, options);
}
}
privatevoidcleanCache() {
ArrayList<T> deleteQueue = newArrayList<>();
Collection<Marker> clusterMarkers = mClusterManager
.getClusterMarkerCollection().getMarkers();
for (T clusterItem : mClusterMap.keySet()) {
if (!clusterMarkers.contains(mClusterMap.get(clusterItem))) {
deleteQueue.add(clusterItem);
}
}
for (T clusterItem : deleteQueue) {
mClusterMap.remove(clusterItem);
}
deleteQueue.clear();
}
private MarkerOptions getMarkerOptionsFromMarker(@NonNull Marker marker) {
MarkerOptionsoptions=newMarkerOptions();
options.alpha(marker.getAlpha());
options.draggable(marker.isDraggable());
options.flat(marker.isFlat());
options.position(marker.getPosition());
options.rotation(marker.getRotation());
options.title(marker.getTitle());
options.snippet(marker.getSnippet());
options.visible(marker.isVisible());
options.zIndex(marker.getZIndex());
return options;
}
privatevoidloadMarkerWithMarkerOptions(@NonNull Marker marker,
@NonNull MarkerOptions options) {
marker.setAlpha(options.getAlpha());
marker.setDraggable(options.isDraggable());
marker.setFlat(options.isFlat());
marker.setPosition(options.getPosition());
marker.setRotation(options.getRotation());
marker.setTitle(options.getTitle());
marker.setSnippet(options.getSnippet());
marker.setVisible(options.isVisible());
marker.setZIndex(options.getZIndex());
marker.setIcon(options.getIcon());
marker.setAnchor(options.getAnchorU(), options.getAnchorV());
marker.setInfoWindowAnchor(options.getInfoWindowAnchorU(), options.getInfoWindowAnchorV());
}
}
Solution 5:
I had the same problem. It was also compounded by the fact I'm doing custom rendering in onBeforeClusterItemRendered on my DefaultClusterRenderer subclass.
My solution was to create a new instance of my DefaultClusterRenderer subclass and call setRenderer on the ClusterManager again. This dumps all the cached icons & recreates everything.
It's hacky, brute force and annoyingly inefficient, but it does work. It was the only approach I found that worked since the library seems to have no explicit support for this.
Post a Comment for "Clustermanager Repaint Markers Of Google Maps V2 Utils"