Search code examples
javascriptgoogle-mapsgoogle-maps-api-3markerclusterer

Remove pins when clustering google map


I am having an issue with google map clustering pins. The problem is that after clustering is done the pins don't dissapear.

array here

var myLatLng = new google.maps.LatLng(lat,lng);
var marker_new = new google.maps.Marker({
                    position: myLatLng,
                    map: map
                  });
new_markers.push(marker_new);

after array is done

clusterStyles = [
        {
        textColor: '#ffffff',    
        opt_textColor: '#ffffff',
        url: mapfunctions_vars.path+'/cloud.png',
        height: 72,
        width: 72,
        textSize:15,

        }
    ];
    mcOptions = {
        gridSize: 100,
        ignoreHidden:true, 
        maxZoom: mapfunctions_vars.zoom_cluster, 
        styles: clusterStyles
    };
    var mcsluster;

    mcsluster = new MarkerClusterer(map, new_markers, mcOptions);
    mcsluster.setIgnoreHidden(true);

Any ideas how i can remove them ?


Solution

  • Don't add your markers to the map, merely create them with coordinates. Just change:

    var marker_new = new google.maps.Marker({
        position: myLatLng,
        map: map
    });
    

    to:

    var marker_new = new google.maps.Marker({
        position: myLatLng
    });
    

    You can see this in the example in the documentation: https://googlemaps.github.io/js-marker-clusterer/examples/simple_example.html

    for (var i = 0; i < 100; i++) {
        var dataPhoto = data.photos[i];
        var latLng = new google.maps.LatLng(dataPhoto.latitude,
            dataPhoto.longitude);
        var marker = new google.maps.Marker({
            position: latLng
        });
        markers.push(marker);
    }
    var markerCluster = new MarkerClusterer(map, markers);