现在您可以将自己的渲染器传递给MarkerClusterer对象,该对象返回一个类似于google.maps.Marker的标记:
const renderer = {
render({ count, position }) {
return new google.maps.Marker({
label: { text: String(count), color: "white", fontSize: "10px" },
position,
zIndex: Number(google.maps.Marker.MAX_ZINDEX) + count,
});
}
}
const markerCluster = new MarkerClusterer({ map, markers, renderer });
在render方法中,您可以像定制普通标记一样定制聚合标记。有关更多详细信息,请参见https://developers.google.com/maps/documentation/javascript/custom-markers。例如,如果您只想添加特定的icon.png,则可以按照以下方式进行操作:
const icon = {
url: '/path/to/your/icon.png',
scaledSize: new google.maps.Size(50, 50),
};
const renderer = {
render({ count, position }) {
return new google.maps.Marker({
label: { text: String(count), color: "white", fontSize: "10px" },
position,
icon,
zIndex: Number(google.maps.Marker.MAX_ZINDEX) + count,
});
}
}
您还可以在
https://googlemaps.github.io/js-markerclusterer/public/renderers/找到一些聚类标记渲染示例。
请参见
https://developers.google.com/maps/documentation/javascript/marker-clustering,了解如何设置MarkerCluster的完整示例。
历史记录详情:
MarkerCluster库已于2021年进行重写。其中一个目标是更改呈现过程。集群图标本身现在是一个google.maps.Marker,因此可以像普通标记一样进行样式设置。构造函数中的
mcOptions
已经不存在了。
建议的设计是将聚类和渲染的逻辑封装到开发人员可以扩展的接口中,特别是算法和渲染器。算法计算聚类,渲染器生成一个google.maps.Marker来表示聚类,而不是使用google.maps.OverlayView。