Class MarkerClusterer

java.lang.Object
com.codename1.maps.MarkerClusterer
All Implemented Interfaces:
CameraChangeListener

public class MarkerClusterer extends Object implements 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();
  • Constructor Details

    • MarkerClusterer

      public MarkerClusterer(MapSurface map)

      Creates a clusterer and listens for camera changes of the map.

      Parameters
      • map: 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: 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

      public void addMarker(MarkerOptions options)

      Adds a marker. Call cluster() after adding a batch.

      Parameters
      • options: the marker
    • removeMarker

      public void removeMarker(MarkerOptions options)

      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

      public void cameraChanged(MapSurface surface, CameraPosition position)
      Description copied from interface: CameraChangeListener

      Invoked 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:
      cameraChanged in interface CameraChangeListener
    • 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: the positions

      • zoom: the integer zoom level

      • gridSize: the cell size in pixels

      Returns

      the clusters, in order of first appearance; singletons included