Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to hide and show MarkerClusterer in google maps

i'm trying to hide/show markerClusterer when user clicks some buttons:

Here is what i'm trying to do:

    map = new google.maps.Map(document.getElementById("mappa"),mapOptions);
    var marker_tmp = [];
    var markers_tmp = [];
    $.each(json,function(index,value){
        var latLng = new google.maps.LatLng(value.lat,value.lng);
        var marker = new google.maps.Marker({'position': latLng});
        if((value.candidato in markers_tmp)==false){
            markers_tmp[value.name]=[];
        }
        markers_tmp[value.name].push(marker);
    });
    for(var name in markers_tmp){
        markers[name]= new MarkerClusterer(map,markers_tmp[name]);
    }

I create multiple markerClusterer each one is associated to a particular name.

So i have some buttons associated to these particular name and i need to hide/show the marker clusterer associated with that button.

/*This is the function associated to a button when it is clicked*/
function hide_show_cluster(name,visible){
    var tmp_cluster = markers[name];
    //call a function of markerClusterer (tmp_cluster) to hide/show it
}

I've done lots of tests but no one satisfy my request. Can someone help me? Thanks!

like image 438
Jayyrus Avatar asked Feb 15 '13 12:02

Jayyrus


People also ask

How do I hide widgets on Google Maps?

Turn off widgetsTap and hold the Google Maps widget. Tap Edit widget. Turn the widget off.

How do I remove a marker cluster from Google Maps?

You can delete the markers by removing them from the map and then setting the array's length to 0 , which removes all references to the markers.

How do I hide markers on Google Maps API?

Once you are able to detect the marker click event you need to "hide" or remove the marker from the map. The standard way for doing this with google maps is to do this: this. setMap(null);


2 Answers

I've been struggling the whole morning with this but fortunately I got to a solution.

First of all, make sure you have the latest MarkerClustererPlus version https://github.com/googlemaps/js-marker-clusterer

then it is very easy,

When creating the markers make sure you

set its visible flag to false.

And when creating the marker clusterer do it this way:

new MarkerClusterer(map, markers, { ignoreHidden: true });

if you want to show the clusterer just do this:

for (var it in markers) {
    markers[it].setVisible(true);
}

markerCluster.repaint();

to hide the cluster:

for (var it in markers) {
    markers[it].setVisible(false);
}

markerCluster.repaint();

Hope it helps, regards

like image 79
nicoabie Avatar answered Sep 17 '22 16:09

nicoabie


I have the same case and this is how I do it... hope it helps

cluster instanciate

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

to hide the clusters:

function hideMarkers() {
    for (let i in markers) {
        markers[i].setMap(null);
    }
    markerCluster.clearMarkers();
}

to show the clusters:

function showMarkers() {
    for (let i in markers) {
        markers[i].setMap(map);
    }
    markerCluster.addMarkers(markers);
}

here is a jsfiddle link to test http://jsfiddle.net/3s6qfzcy/

like image 25
Abdul Manaf Saparuddin Avatar answered Sep 18 '22 16:09

Abdul Manaf Saparuddin