Marker clusters
It’s possible to display overlapping user markers as clusters using GeoMarkerClusterManager
.
First, create an instance of GeoMarkerClusterManager
using MapView.createMarkerClusterManager
method:
GeoMarkerClusterManager markerClusterManager = mapView.createMarkerClusterManager();
To add markers, use addGeoMarker
or addGeoMarkers
methods:
markerClusterManager.addGeoMarker(marker);
Markers are removed using removeGeoMarker
and removeGeoMarkers
methods. The method removeAllGeoMarkers
is available to remove all markers at once.
The destroy
method removes all markers from the map and releases all resources associated with the manager. Once this method is called, the manager should not be used.
Customizing Cluster Appearance
It's possible to customize the appearance of markers and clusters. The setImage
method allows changing the cluster image. Adjustments to the item count labels on clusters, like font size, color, and offset, can be made using setLabelFontSize
, setLabelColor
, and setLabelOffset
.
Example:
markerClusterManager.setImage(
ContextCompat.getDrawable(GeoObjectsActivity.this, android.R.drawable.ic_menu_gallery),
new PointF(0.5f, 0.5f));
markerClusterManager.setShowItemCount(true);
markerClusterManager.setLabelFontSize(16);
markerClusterManager.setLabelColor(Color.BLUE);
Interacting with Clusters
Retrieving clusters at specific screen positions is possible using getClustersAtPoint
. The returned clusters will be ordered by their distance from the specified point.
If an object implementing GeoObjectListener
interface is added to a map view using MapView.addGeoObjectListener
method, clicking on clusters will result in the corresponding callbacks being called, with GeoMarkerCluster
objects passed as arguments.
GeoMarkerCluster
contains methods that allow retrieving its position, bounds, the number of markers it contains and the markers themselves. This can be used, for example, to change the camera position when a cluster is clicked: