MarkerCluster
AMap.MarkerCluster
Used to display a large number of point markers, clustering them by distance to improve rendering performance. Point clustering supports user-defined styles and is called as a plugin. Construct a point cluster object and set its properties through MarkerClusterOptions
new AMap.MarkerCluster(map: Map, dataOptions: Array, MarkerClusterOptions: Object)Parameter
map (Map) The map object to which point clustering needs to be added
dataOptions (Array) The point data that needs to be displayed in clusters
MarkerClusterOptions (Object) Point aggregation property settings
Demo
// The data format is an array containing latitude and longitude information, as shown below. Among them, [latitude and longitude] lnglat is a required field, and [weight] weight is an optional field.
var points = [
{ weight: 8, lnglat: ["108.939621", "34.343147"] },
{ weight: 1, lnglat: ["112.985037", "23.15046"] },
{ weight: 1, lnglat: ["110.361899", "20.026695"] },
{ weight: 1, lnglat: ["121.434529", "31.215641"] },
];
// Load point cluster plugin
map.plugin(["AMap.MarkerCluster"], function () {
var cluster = new AMap.MarkerCluster(map, points, {
gridSize: 80, // Cluster grid pixel size
});
});Method
addData(dataOptions)
Add data based on the original data, with the same format as dataOptions
Parameter: dataOptions (dataOptions) Added data, such as: [{ weight: 1, lnglat: [\"116.105381\", \"39.937183\"] }, ...]
Demo:
cluster.addData([{ weight: 1, lnglat: ["116.195445", "39.914601"] }]);setData(dataOptions)
Set cluster point data, this method will reset the original data, the format is the same as dataOptions
Parameter: dataOptions (dataOptions) Set data, such as: [{ weight: 1, lnglat: [\"116.105381\", \"39.937183\"] }, ...]
Demo:
cluster.setData([{ weight: 1, lnglat: ["116.195445", "39.914601"] }]);getClustersCount()
Get the total number of aggregation points at the current zoom level
return value: (Number) Total number of aggregation points
Demo:
cluster.getClustersCount();getGridSize()
Set the pixel size of the aggregation grid
return value: (Number) Pixel size of the aggregation grid
Demo:
cluster.getGridSize();setGridSize(size)
Get the pixel size of the aggregation grid
Parameter: size(Number) Pixel Size
Demo:
cluster.setGridSize(40);getMaxZoom()
Get the Maximum Aggregation Level of Point Markers on the Map
return value: (Number) Maximum Aggregation Level of Point Markers
Demo:
cluster.getMaxZoom();setMaxZoom(zoom)
Set the Maximum Aggregation Level of Point Markers on the Map
Parameter: zoom (Number) Maximum Aggregation Level
Demo:
cluster.setMaxZoom(18);setStyles(Map)
Set the style aggregation point of the styles attribute, the format is the same as opts.styles, this attribute is invalid when there are valid renderClusterMarker and renderMarker settings
Parameter: Map (Array<Object>) Icon style
Demo:
cluster.setStyles([
{
//The style of the aggregation point when the aggregation quantity is 1-10
url: "//a.amap.com/jsapi_demos/static/images/blue.png", //The address of the image displayed by the icon
size: new AMap.Size(32, 32), //The size of the image displayed by the icon
offset: new AMap.Pixel(-16, -16), //The offset value of the icon's position on the map relative to the upper-left corner of the icon
textColor: "#fff", //The color of the text
},
{
//The style of aggregation points when the aggregation quantity is between 11-100
url: "//a.amap.com/jsapi_demos/static/images/green.png",
size: new AMap.Size(32, 32),
offset: new AMap.Pixel(-16, -16),
textColor: "#fff",
},
{
//The style of aggregation points when the aggregation quantity is between 101-1000
url: "//a.amap.com/jsapi_demos/static/images/orange.png",
size: new AMap.Size(36, 36),
offset: new AMap.Pixel(-18, -18),
},
]);getStyles()
Get the icon style of point marker set by styles
return value: (Array<Object>) Icon style of point marker
Demo:
cluster.getStyles();isAverageCenter()
Get whether the position of a single cluster point is the average center of all markers within the cluster
return value: (Boolean) Boolean value
Demo:
cluster.isAverageCenter();setAverageCenter(averageCenter)
Set whether the cluster point position is the center of all cluster points
Parameter: averageCenter (Boolean) If set to true, the average position of all internal markers will be calculated, and the position of the cluster point will be updated to this average position. If set to false, the position of the cluster point will not be set to the average center of all internal markers
Demo:
cluster.setAverageCenter(true);Event
For event object property descriptions, go to:MapsEvent