Skip to content

Google Maps clustering if markers greater than 5

I’m trying to add clusters into google map and below code works charm, but I need to add clusters if there are more than 5 markers at same place. How would I do that with 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'});
}

Answer

From the MarkerClustererPlus documentation

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.

To set it to 5:

new MarkerClusterer(map, markers, {minimumClusterSize: 5, imagePath: 'https://developers.google.com/maps/documentation/javascript/examples/markerclusterer/m'});