How to show InfoWindow using Android Maps Utility Library for Android

前端 未结 1 976
执念已碎
执念已碎 2021-02-03 12:52

I\'m using Google Maps Android API Utility Library in order to show several markers in a map in a clustered way. I\'ve followed the instructions to make it work, as well as taki

相关标签:
1条回答
  • 2021-02-03 13:19

    You need to extend the DefaultClusterRenderer class and override the onBeforeClusterItemRendered, attaching the title to the MarkerOptions object passed as argument.

    After that, you can pass your implementation to the ClusterManager.

    Example:

    class MyItem implements ClusterItem {
    
        private LatLng mPosition;
        private String mTitle;
    
        public MyItem(LatLng position){
            mPosition = position;
        }
    
        @Override
        public LatLng getPosition() {
            return mPosition;
        }
    
        public String getTitle() {
            return mTitle;
        }
    
        public void setTitle(String title) {
            mTitle = title;
        }
    }
    
    class MyClusterRenderer extends DefaultClusterRenderer<MyItem> {
    
        public MyClusterRenderer(Context context, GoogleMap map,
                ClusterManager<MyItem> clusterManager) {
            super(context, map, clusterManager);
        }
    
        @Override
        protected void onBeforeClusterItemRendered(MyItem item, MarkerOptions markerOptions) {
            super.onBeforeClusterItemRendered(item, markerOptions);
    
            markerOptions.title(item.getTitle());
        }
    
        @Override
        protected void onClusterItemRendered(MyItem clusterItem, Marker marker) {
            super.onClusterItemRendered(clusterItem, marker);
    
            //here you have access to the marker itself
        }
    }
    

    And then you can use it in this way:

    ClusterManager<MyItem> clusterManager = new ClusterManager<MyItem>(this, getMap());
    clusterManager.setRenderer(new MyClusterRenderer(this, getMap() ,clusterManager));
    
    0 讨论(0)
提交回复
热议问题