Client API. The Codename One framework your app is built on: this runs on the device, not in a backend.
public class MarkerClusterer
- Object
- 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.Cluster | A 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
mapMapSurface- a
MapViewor anotherMapSurface
Method details
setGridSize
public void setGridSize(int gridSize)Sets the cell size in screen pixels. The default is 60.
Parameters
gridSizeint- the cell size
setMaxZoom
public void setMaxZoom(int maxZoom)Sets the zoom level above which markers are never clustered. The default is 18.
Parameters
maxZoomint- the zoom level
setMinClusterSize
public void setMinClusterSize(int minClusterSize)Sets the smallest group shown as a cluster. The default is 2.
Parameters
minClusterSizeint- the minimum count
addMarker
public void addMarker(MarkerOptions options)Adds a marker. Call
cluster() after adding a batch.Parameters
optionsMarkerOptions- the marker
removeMarker
public void removeMarker(MarkerOptions options)Removes a marker added with
addMarker.Parameters
optionsMarkerOptions- 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
surfaceMapSurface- the surface whose camera moved
positionCameraPosition- 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
pointsList<LatLng>- the positions
zoomint- the integer zoom level
gridSizeint- the cell size in pixels
Returns
the clusters, in order of first appearance; singletons included