我正在尝试将簇添加到Google Map中,下面的代码工作魅力,但是如果同一地点有超过5个标记,我需要添加簇。我将如何使用MarkerClusterer
?
function map(data, baseLat, baseLng){
var markers = [];
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 10,
center: new google.maps.LatLng(baseLat, baseLng),
mapTypeId: google.maps.MapTypeId.ROADMAP
});
var infowindow = new google.maps.InfoWindow();
$.each(data, function(i, val) {
var marker = new google.maps.Marker({
position: new google.maps.LatLng(val.property.lat, val.property.lng),
//map: map,
title: "Title"
});
markers.push(marker);
});
new MarkerClusterer(map, markers, {imagePath: 'https://developers.google.com/maps/documentation/javascript/examples/markerclusterer/m'});
}
来自MarkerClustererPlus文档
最小值数字
集群中需要的最小标记数 在隐藏标记并出现群集标记之前。默认值是
2
。
将其设置为5
:
new MarkerClusterer(map, markers, {minimumClusterSize: 5, imagePath: 'https://developers.google.com/maps/documentation/javascript/examples/markerclusterer/m'});
MarkerClustererPlus已重新制作到 @googlemaps/markerclusterer中。
要在新库中设置最小群集大小,可以将选项传递给构造函数。
来源:https://github.com/googlemaps/js-markerclusterer/issues/111
import {MarkerClusterer, SuperClusterAlgorithm} from '@googlemaps/markerclusterer';
new MarkerClusterer({
algorithm: new SuperClusterAlgorithm({minPoints: 1234}),
})