androidandroid-maps-v2android-maps-utils

ClusterManager repaint markers of Google maps v2 utils


I'm making a server request and when I receive the response from server, I'm executing on UI Thread a ClusterManager.addItem() but this items are not painting in the map, only when I make a zoom update (+,-) newly added items starts appearing. I also tried to debug the renderer, but onBeforeClusterRendered / onBeforeClusterItemRendered are not getting called until I update the zoom in map.

Any ideas how to refresh map/clusterManager/markers?

MarkerManager markerManager = new MarkerManager(map);
clusterManager = new ClusterManager<TweetClusterItem>(getActivity(), map, markerManager);
clusterManager.setRenderer(new TweetClusterRenderer(getActivity(), map, clusterManager, defaultMarker));
clusterManager.setOnClusterClickListener(this);
clusterManager.setOnClusterInfoWindowClickListener(this);
clusterManager.setOnClusterItemClickListener(this);
clusterManager.setOnClusterItemInfoWindowClickListener(this);

UiSettings uiSettings = map.getUiSettings();
uiSettings.setZoomControlsEnabled(true);
uiSettings.setMyLocationButtonEnabled(false);

map.setOnCameraChangeListener(clusterManager);
map.setOnMarkerClickListener(clusterManager);
map.setOnInfoWindowClickListener(clusterManager);
map.setOnMapClickListener(this);

Solution

  • 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:

    @Override
    protected void onBeforeClusterItemRendered(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
     */
      public void reloadMarker(TweetClusterItem item) {
    
            MarkerManager.Collection markerCollection = clusterManager.getMarkerCollection();
            Collection<Marker> markers = markerCollection.getMarkers();
            String strId = 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 :)