Showing custom InfoWindow for Android Maps Utility Library for Android

后端 未结 2 1696
生来不讨喜
生来不讨喜 2020-12-07 15:42

I\'m using the library Google Maps Utility for Android which allows to create clustering int he maps and I need to show a custom InfoWindow but I can\'t find an

2条回答
  •  天涯浪人
    2020-12-07 16:23

    Yes, this can be done. ClusterManager maintains two MarkerManager.Collections:

    • one for cluster markers, and
    • one for individual item markers

    You can set a custom InfoWindowAdapter for each of these kinds of markers independently.


    Implementation

    First, install your ClusterManager's MarkerManager as the map's InfoWindowAdapter:

    ClusterManager clusterMgr = new ClusterManager(context, map);
    map.setInfoWindowAdapter(clusterMgr.getMarkerManager());
    

    Next, install your custom InfoWindowAdapter as the adapter for one or both of the marker collections:

    clusterMgr.getClusterMarkerCollection().setOnInfoWindowAdapter(new MyCustomAdapterForClusters());
    clusterMgr.getMarkerCollection().setOnInfoWindowAdapter(new MyCustomAdapterForItems());
    

    The final piece is mapping the raw Marker object that you'll receive in your custom InfoWindowAdapter's callback to the ClusterItem object(s) that you added to the map in the first place. This can be achieved using the onClusterClick and onClusterItemClick listeners, as follows:

    map.setOnMarkerClickListener(clusterMgr);
    clusterMgr.setOnClusterClickListener(new OnClusterClickListener() {
        @Override
        public boolean onClusterClick(Cluster cluster) {
            clickedCluster = cluster; // remember for use later in the Adapter
            return false;
        }
    });
    clusterMgr.setOnClusterItemClickListener(new OnClusterItemClickListener() {
        @Override
        public boolean onClusterItemClick(MarkerItem item) {
            clickedClusterItem = item;
            return false;
        }
    });
    

    Now you have everything you need to assemble your custom InfoWindow content in your respective Adapters! For example:

    class MyCustomAdapterForClusters implements InfoWindowAdapter {
        @Override
        public View getInfoContents(Marker marker) {
            if (clickedCluster != null) {
                for (MarkerItem item : clickedCluster.getItems()) {
                    // Extract data from each item in the cluster as needed
                }
            }
            // build your custom view
            // ...
            return view;
        }
    }
    

提交回复
热议问题