Class MarkerClusterer
- All Implemented Interfaces:
CameraChangeListener
Groups nearby markers into one cluster marker labeled with the count, and regroups them when the zoom level changes.
Clustering is grid based in world pixels: at each integer zoom level the
world is cut into square cells of gridSize pixels, and the markers in a
cell form one cluster at their average position. Panning does not change
the clusters, so only a zoom change regroups. Tapping a cluster zooms in
two levels on it. Above maxZoom every marker shows on its own.
MapView map = new MapView();
MarkerClusterer clusterer = new MarkerClusterer(map);
for (int i = 0; i < 100; i++) {
clusterer.addMarker(new MarkerOptions(new LatLng(32 + i * 0.01, 34.8 + i * 0.01)).title("Stop " + i));
}
clusterer.cluster();
-
Nested Class Summary
Nested Classes -
Constructor Summary
ConstructorsConstructorDescriptionCreates a clusterer and listens for camera changes of the map. -
Method Summary
Modifier and TypeMethodDescriptionvoidaddMarker(MarkerOptions options) Adds a marker.voidcameraChanged(MapSurface surface, CameraPosition position) Invoked on the EDT after the camera settles at a new position, whether from a programmatic move or a user pan/zoom gesture.voidclear()Removes every marker from the clusterer and the map.voidcluster()Regroups the markers for the current zoom level and updates the map.static List<MarkerClusterer.Cluster> Groups points by grid cell at a zoom level.voiddispose()Stops listening to the map and removes the markers this clusterer added.voidremoveMarker(MarkerOptions options) Removes a marker added withaddMarker.voidsetGridSize(int gridSize) Sets the cell size in screen pixels.voidsetMaxZoom(int maxZoom) Sets the zoom level above which markers are never clustered.voidsetMinClusterSize(int minClusterSize) Sets the smallest group shown as a cluster.
-
Constructor Details
-
MarkerClusterer
Creates a clusterer and listens for camera changes of the map.
Parameters
map: aMapViewor anotherMapSurface
-
-
Method Details
-
setGridSize
public void setGridSize(int gridSize) Sets the cell size in screen pixels. The default is 60.
Parameters
gridSize: the cell size
-
setMaxZoom
public void setMaxZoom(int maxZoom) Sets the zoom level above which markers are never clustered. The default is 18.
Parameters
maxZoom: the zoom level
-
setMinClusterSize
public void setMinClusterSize(int minClusterSize) Sets the smallest group shown as a cluster. The default is 2.
Parameters
minClusterSize: the minimum count
-
addMarker
Adds a marker. Call
cluster()after adding a batch.Parameters
options: the marker
-
removeMarker
Removes a marker added with
addMarker.Parameters
options: the marker
-
clear
public void clear()Removes every marker from the clusterer and the map. -
dispose
public void dispose()Stops listening to the map and removes the markers this clusterer added. -
cameraChanged
Description copied from interface:CameraChangeListenerInvoked on the EDT after the camera settles at a new position, whether from a programmatic move or a user pan/zoom gesture.
Parameters
-
map: the surface whose camera moved -
position: the new camera position
- Specified by:
cameraChangedin interfaceCameraChangeListener
-
-
cluster
public void cluster()Regroups the markers for the current zoom level and updates the map. -
cluster
Groups points by grid cell at a zoom level. Pure math; it touches no map.
Parameters
-
points: the positions -
zoom: the integer zoom level -
gridSize: the cell size in pixels
Returns
the clusters, in order of first appearance; singletons included
-
-