Client API. The Codename One framework your app is built on: this runs on the device, not in a backend.

public class MarkerClusterer

  1. Object
  2. MarkerClusterer

ImplementsCameraChangeListener

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 types

class MarkerClusterer.ClusterA group of markers.

Constructors

public MarkerClusterer(MapSurface map)Creates a clusterer and listens for camera changes of the map.

Methods

public void setGridSize(int gridSize)Sets the cell size in screen pixels.
public void setMaxZoom(int maxZoom)Sets the zoom level above which markers are never clustered.
public void setMinClusterSize(int minClusterSize)Sets the smallest group shown as a cluster.
public void addMarker(MarkerOptions options)Adds a marker.
public void removeMarker(MarkerOptions options)Removes a marker added with addMarker.
public void clear()Removes every marker from the clusterer and the map.
public void dispose()Stops listening to the map and removes the markers this clusterer added.
public void cameraChanged(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.
public void cluster()Regroups the markers for the current zoom level and updates the map.
public static List<MarkerClusterer.Cluster> cluster(List<LatLng> points, int zoom, int gridSize)Groups points by grid cell at a zoom level.

Inherited methods

Constructor details

MarkerClusterer

public MarkerClusterer(MapSurface map)
Creates a clusterer and listens for camera changes of the map.

Parameters

map MapSurface
a MapView or another MapSurface

Method details

setGridSize

public void setGridSize(int gridSize)
Sets the cell size in screen pixels. The default is 60.

Parameters

gridSize int
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 int
the zoom level

setMinClusterSize

public void setMinClusterSize(int minClusterSize)
Sets the smallest group shown as a cluster. The default is 2.

Parameters

minClusterSize int
the minimum count

addMarker

public void addMarker(MarkerOptions options)
Adds a marker. Call cluster() after adding a batch.

Parameters

options MarkerOptions
the marker

removeMarker

public void removeMarker(MarkerOptions options)
Removes a marker added with addMarker.

Parameters

options MarkerOptions
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

public void cameraChanged(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.

Parameters

surface MapSurface
the surface whose camera moved
position CameraPosition
the new camera position

cluster

public void cluster()
Regroups the markers for the current zoom level and updates the map.

cluster

public static List<MarkerClusterer.Cluster> cluster(List<LatLng> points, int zoom, int gridSize)
Groups points by grid cell at a zoom level. Pure math; it touches no map.

Parameters

points List<LatLng>
the positions
zoom int
the integer zoom level
gridSize int
the cell size in pixels

Returns

the clusters, in order of first appearance; singletons included