How to add title, snippet and icon to ClusterItem?

后端 未结 1 946
闹比i
闹比i 2020-12-04 23:02

I had this activity:

public class MapViewer extends Activity {

    private GoogleMap map;
    private Database db = new Database(this);

    protected void          


        
相关标签:
1条回答
  • 2020-12-04 23:50

    Here you should work with ClusterManager itself. For example, setting on cluster item click:

            mClusterManager.setOnClusterItemClickListener(new ClusterManager.OnClusterItemClickListener<MyItem>() {
                @Override
                public boolean onClusterItemClick(MyItem item) {
                    //put your code here
                    return false;
                }
            });
    

    And there are other different methods in ClusterManager class. You'll need a less obvious code to assign info window to marker or cluster - you should use this code:

    mClusterManager.getMarkerCollection().setOnInfoWindowAdapter(new MarkerInfoWindowAdapter());
    mClusterManager.getClusterMarkerCollection().setOnInfoWindowAdapter(new ClusterInfoWindow());
    

    There MarkerInfoWindowAdapter and ClusterWindowAdapter is your classes, which implements GoogleMap.InfoWindowAdapter.

    With icons it's a little harder, because clustering changes icons to default. You should use method:

    public void setRenderer(com.google.maps.android.clustering.view.ClusterRenderer<T> view)
    

    You could your own renderer class and override some methods. For example, to set custom icons use such a class:

    class OwnIconRendered extends DefaultClusterRenderer<MyItem> {
    
        public OwnIconRendered(Context context, GoogleMap map,
                               ClusterManager<MyItem> clusterManager) {
            super(context, map, clusterManager);
        }
    
        @Override
        protected void onBeforeClusterItemRendered(MyItem item, MarkerOptions markerOptions) {
            markerOptions.icon(item.getIcon());
            markerOptions.snippet(item.getSnippet());
            markerOptions.title(item.getTitle());
            super.onBeforeClusterItemRendered(item, markerOptions);
        }
    }
    

    And use it in such way:

    mClusterManager.setRenderer(new OwnIconRendered(activity.getApplicationContext(), getMap(), mClusterManager));
    
    0 讨论(0)
提交回复
热议问题