简体   繁体   中英

MarkerClusterer is marker in cluster?

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 (ie 5 infoboxes have to be visible)?

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 completes 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 completes,
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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM