MarkerClustererPlus for Google Maps V3

The library creates and manages per-zoom-level clusters for large amounts of markers.

This is an enhanced V3 implementation of the V2 MarkerClusterer by Xiaoxi Wu. It is based on the V3 MarkerClusterer port by Luke Mahe. MarkerClustererPlus was created by Gary Little.

v2.0 release: MarkerClustererPlus v2.0 is backward compatible with MarkerClusterer v1.0. It adds support for the ignoreHidden, title, printable, batchSizeIE, and calculator properties as well as support for four more events. It also allows greater control over the styling of the text that appears on the cluster marker. The documentation has been significantly improved and the overall code has been simplified and polished. Very large numbers of markers can now be managed without causing Javascript timeout errors on Internet Explorer. Note that the name of the clusterclick event has been deprecated. The new name is click, so please change your application code now.

For a description and examples of how to use this library, check out the how-to.

class Cluster

Constructor

Constructor Description
Cluster(mc:MarkerClusterer) Creates a single cluster that manages a group of proximate markers. Used internally, do not call this constructor directly.

Methods

Methods Return Value Description
getCenter() google.maps.LatLng Returns the center of the cluster. You can call this from a click, mouseover, or mouseout event handler for the MarkerClusterer object.
getMarkers() Array Returns the array of markers managed by the cluster. You can call this from a click, mouseover, or mouseout event handler for the MarkerClusterer object.
getSize() number Returns the number of markers managed by the cluster. You can call this from a click, mouseover, or mouseout event handler for the MarkerClusterer object.

class ClusterIconInfo

This class is an object containing general information about a cluster icon. This is the object that a calculator function returns.

Properties

Properties Type Description
index number The index plus 1 of the element in the styles array to be used to style the cluster icon.
text string The text of the label to be shown on the cluster icon.

class ClusterIconStyle

This class represents the object for values in the styles array passed to the MarkerClusterer constructor. The element in this array that is used to style the cluster icon is determined by calling the calculator function.

Properties

Properties Type Description
anchor Array The anchor position (in pixels) of the label text to be shown on the cluster icon, relative to the top left corner of the icon. The format is [yoffset, xoffset]. The yoffset must be positive and less than height and the xoffset must be positive and less than width. The default is to anchor the label text so that it is centered on the icon.
backgroundPosition string The position of the cluster icon image within the image defined by url. The format is "xpos ypos" (the same format as for the CSS background-position property). You must set this property appropriately when the image defined by url represents a sprite containing multiple images. The default value is "0 0".
fontFamily number The value of the CSS font-family property for the label text shown on the cluster icon. The default value is "Arial,sans-serif".
fontStyle number The value of the CSS font-style property for the label text shown on the cluster icon. The default value is "normal".
fontWeight number The value of the CSS font-weight property for the label text shown on the cluster icon. The default value is "bold".
height number The height (in pixels) of the cluster icon. Required.
textColor string The color of the label text shown on the cluster icon. The default value is "black".
textDecoration number The value of the CSS text-decoration property for the label text shown on the cluster icon. The default value is "none".
textSize number The size (in pixels) of the label text shown on the cluster icon. The default value is 11.
url string The URL of the cluster icon image file. Required.
width number The width (in pixels) of the cluster icon. Required.

class MarkerClusterer

This class extends google.maps.OverlayView.

Constructor

Constructor Description
MarkerClusterer(map:google.maps.Map, opt_markers?:Array.<google.maps.Marker>, opt_options?:MarkerClustererOptions) Creates a MarkerClusterer object with the options specified in MarkerClustererOptions.

Methods

Methods Return Value Description
addMarker(marker:google.maps.Marker, opt_nodraw?:boolean) None Adds a marker to the clusterer. The clusters are redrawn unless opt_nodraw is set to true.
addMarkers(markers:Array.<google.maps.Marker>, opt_nodraw?:boolean) None Adds an array of markers to the clusterer. The clusters are redrawn unless opt_nodraw is set to true.
clearMarkers() None Removes all clusters and markers from the map and also removes all markers managed by the clusterer.
fitMapToMarkers() None Fits the map to the bounds of the markers managed by the clusterer.
getAverageCenter() boolean Returns the value of the averageCenter property.
getBatchSizeIE() number Returns the value of the batchSizeIE property.
getCalculator() function Returns the value of the calculator property.
getGridSize() number Returns the value of the gridSize property.
getIgnoreHidden() boolean Returns the value of the ignoreHidden property.
getImageExtension() string Returns the value of the imageExtension property.
getImagePath() string Returns the value of the imagePath property.
getImageSizes() Array Returns the value of the imageSizes property.
getMarkers() Array Returns the array of markers managed by the clusterer.
getMaxZoom() number Returns the value of the maxZoom property.
getMinimumClusterSize() number Returns the value of the minimumClusterSize property.
getPrintable() boolean Returns the value of the printable property.
getStyles() Array Returns the value of the styles property.
getTitle() string Returns the value of the title property.
getTotalClusters() number Returns the number of clusters formed by the clusterer.
getTotalMarkers() number Returns the number of markers managed by the clusterer.
getZoomOnClick() boolean Returns the value of the zoomOnClick property.
removeMarker(marker:google.maps.Marker, opt_nodraw?:boolean) boolean Removes a marker from the cluster. The clusters are redrawn unless opt_nodraw is set to true. Returns true if the marker was removed from the clusterer.
removeMarkers(markers:Array.<google.maps.Marker>, opt_nodraw?:boolean) boolean Removes an array of markers from the cluster. The clusters are redrawn unless opt_nodraw is set to true. Returns true if markers were removed from the clusterer.
repaint() None Recalculates and redraws all the marker clusters from scratch. Call this after changing any properties.
setAverageCenter(averageCenter:boolean) None Sets the value of the averageCenter property.
setBatchSizeIE(batchSizeIE:number) None Sets the value of the batchSizeIE property.
setCalculator(calculator:function(Array.<google.maps.Marker>|number)) None Sets the value of the calculator property.
setGridSize(gridSize:number) None Sets the value of the gridSize property.
setIgnoreHidden(ignoreHidden:boolean) None Sets the value of the ignoreHidden property.
setImageExtension(imageExtension:string) None Sets the value of the imageExtension property.
setImagePath(imagePath:string) None Sets the value of the imagePath property.
setImageSizes(imageSizes:Array) None Sets the value of the imageSizes property.
setMaxZoom(maxZoom:number) None Sets the value of the maxZoom property.
setMinimumClusterSize(minimumClusterSize:number) None Sets the value of the minimumClusterSize property.
setPrintable(printable:boolean) None Sets the value of the printable property.
setStyles(styles:Array.<ClusterIconStyle>) None Sets the value of the styles property.
setTitle(title:string) None Sets the value of the title property.
setZoomOnClick(zoomOnClick:boolean) None Sets the value of the zoomOnClick property.

Events

Events Arguments Description
click c:Cluster This event is fired when a cluster marker is clicked.
clusteringbegin mc:MarkerClusterer This event is fired when the MarkerClusterer begins clustering markers.
clusteringend mc:MarkerClusterer This event is fired when the MarkerClusterer stops clustering markers.
mouseout c:Cluster This event is fired when the mouse moves out of a cluster marker.
mouseover c:Cluster This event is fired when the mouse moves over a cluster marker.

class MarkerClustererOptions

This class represents the optional parameter passed to the MarkerClusterer constructor. There is no constructor for this class. Instead, this class is instantiated as a javascript object literal.

Properties

Properties Type Description
averageCenter boolean Whether the position of a cluster marker should be the average position of all markers in the cluster. If set to false, the cluster marker is positioned at the location of the first marker added to the cluster. The default value is false.
batchSizeIE number When Internet Explorer is being used, markers are processed in several batches with a small delay inserted between each batch in an attempt to avoid Javascript timeout errors. Set this property to the number of markers to be processed in a single batch; select as high a number as you can without causing a timeout error in the browser. This number might need to be as low as 100 if 15,000 markers are being managed, for example. The default value is MarkerClusterer.BATCH_SIZE_IE.
calculator function The function used to determine the text to be displayed on a cluster marker and the index indicating which style to use for the cluster marker. The input parameters for the function are (1) the array of markers represented by a cluster marker and (2) the number of cluster icon styles. It returns a ClusterIconInfo object. The default calculator returns a text property which is the number of markers in the cluster and an index property which is one higher than the lowest integer such that 10^i exceeds the number of markers in the cluster, or the size of the styles array, whichever is less. The styles array element used has an index of index minus 1. For example, the default calculator returns a text value of "125" and an index of 3 for a cluster icon representing 125 markers so the element used in the styles array is 2. The default value is MarkerClusterer.CALCULATOR.
gridSize number The grid size of a cluster in pixels. The grid is a square. The default value is 60.
ignoreHidden boolean Whether to ignore hidden markers in clusters. You may want to set this to true to ensure that hidden markers are not included in the marker count that appears on a cluster marker (this count is the value of the text property of the result returned by the default calculator). If set to true and you change the visibility of a marker being clustered, be sure to also call MarkerClusterer.repaint(). The default value is false.
imageExtension string The extension name for the cluster icon image files (e.g., "png" or "jpg"). The default value is MarkerClusterer.IMAGE_EXTENSION.
imagePath string The full URL of the root name of the group of image files to use for cluster icons. The complete file name is of the form imagePathn.imageExtension where n is the image file number (1, 2, etc.). The default value is MarkerClusterer.IMAGE_PATH.
imageSizes Array An array of numbers containing the widths of the group of imagePathn.imageExtension image files. (The images are assumed to be square.) The default value is MarkerClusterer.IMAGE_SIZES.
maxZoom number The maximum zoom level at which clustering is enabled or null if clustering is to be enabled at all zoom levels. The default value is null.
minimumClusterSize number The minimum number of markers needed in a cluster before the markers are hidden and a cluster marker appears. The default value is 2.
printable boolean Whether to make the cluster icons printable. Do not set to true if the url fields in the styles array refer to image sprite files. The default value is false.
styles Array An array of ClusterIconStyle elements defining the styles of the cluster markers to be used. The element to be used to style a given cluster marker is determined by the function defined by the calculator property. The default is an array of ClusterIconStyle elements whose properties are derived from the values for imagePath, imageExtension, and imageSizes.
title string The tooltip to display when the mouse moves over a cluster marker. The default value is "".
zoomOnClick boolean Whether to zoom the map when a cluster marker is clicked. You may want to set this to false if you have installed a handler for the click event and it deals with zooming on its own. The default value is true.