admin管理员组

文章数量:1325674

I put markers into clusters:

var markerClusterer = new MarkerClusterer(map, markers, {
    zoomOnClick : false,
    maxZoom : 13,
    gridSize : 100
});

And i have 15 markers. 10 of these are in clusters in the map. How to determine if the marker is in a clusters.

var clusteredMarkers = markerClusterer.getTotalMarkers();
for(i = 0; i < clusteredMarkers.length; i++) {
    if(isInCluster((clusteredMarkers[i])) {
        clusteredMarkers[i].infobox.close();        
    }
}

How to define a function like isInCluster(marker) that infobox is open in only markers, that are not in any cluster (i.e. 5 infoboxes have to be visible)?

I put markers into clusters:

var markerClusterer = new MarkerClusterer(map, markers, {
    zoomOnClick : false,
    maxZoom : 13,
    gridSize : 100
});

And i have 15 markers. 10 of these are in clusters in the map. How to determine if the marker is in a clusters.

var clusteredMarkers = markerClusterer.getTotalMarkers();
for(i = 0; i < clusteredMarkers.length; i++) {
    if(isInCluster((clusteredMarkers[i])) {
        clusteredMarkers[i].infobox.close();        
    }
}

How to define a function like isInCluster(marker) that infobox is open in only markers, that are not in any cluster (i.e. 5 infoboxes have to be visible)?

Share edited May 9, 2012 at 11:00 Sean Mickey 7,7262 gold badges33 silver badges58 bronze badges asked May 9, 2012 at 5:47 iffiff 1926 silver badges14 bronze badges 5
  • Which MarkerClusterer library are you using? MarkerClusterer or MarkerClusterePlus, or other? – abidibo Commented May 9, 2012 at 8:05
  • i use markerclusterer.js v3: code.google./p/google-maps-utility-library-v3/source/browse/… – iff Commented May 9, 2012 at 8:28
  • I personally will try to get the clusters this way: var clusters = markerClusterer.clusters_; and then loop and foreach cluster use the getMarkers() method. If it return more than one marker then close their infowindow, else if it return only one marker it not really a cluster – abidibo Commented May 9, 2012 at 8:39
  • It does not help. clusters.length gives 0 instead of 3 but i have 3 clusters containing that contains 2, 3 and 5 markers accordingly. – iff Commented May 9, 2012 at 8:49
  • this is strange, look at line 764 of the source code, the clusters_ member contains the Cluster objects which have the getMarker method. Maybe this array is filled async, can't read the whole code now, try to manually call redraw method and then read the clusters_ property or to read the clusters_ property after a timeout interval – abidibo Commented May 9, 2012 at 9:07
Add a ment  | 

2 Answers 2

Reset to default 8

The MarkerClusterer will set a marker's map to null if it is in a cluster, so that it is no longer displayed on the map. The MarkerClusterer will then set the marker's map back to map anytime it is no longer in a cluster. You could try checking each of your markers:

var mapOptions = {            // Notice that mapZoom is not set
    center: new google.maps.LatLng( 19, 19 ),
    mapTypeId: google.maps.MapTypeId.ROADMAP };

map = new google.maps.Map( document.getElementById( "map_canvas" ), mapOptions );
var markerClusterer = new MarkerClusterer( map, markers, { ... });

//Whenever the map pletes panning or zooming, the function will be called:
google.maps.event.addListener( map, "idle", function() {
    for ( var i = 0; i < markers.length; i++ ) {
        var mrkr = markers[i];
        if ( mrkr.getMap() != null ) {
            mrkr.infobox.open(); 
        }
        else {
            mrkr.infobox.close(); 
        }
    }
}

//Now that map, markerClusterer, and the "idle" event callback are in place,
//set the zoom, which will trigger the "idle" event callback 
//after the zoom activity pletes,
map.setZoom( 19 ); //Or whatever is appropriate

//Thereafter, the callback will run after any pan or zoom...

Obviously, the state of the markers is likely to change after a zoom-in, zoom-out, or any other viewport change, so you will have to re-check after viewport changes.

Look this example http://88.196.132.133/markerclusterer/, it does not work properly with infoboxes. It is necessary that all the markers (if they are out of clusters) is always displayed with infoboxes. The problem is also that all the infoboxes open for a moment when loading a page.

本文标签: javascriptMarkerClusterer is marker in clusterStack Overflow