how to find the markers in the individual clusters in a marker cluster object in google maps api v3?

浪子不回头ぞ 提交于 2021-02-07 08:31:53

问题


I followed the tutorial here to add an inforwindow to the individual clusters of markercluster at any zoom level. http://krisarnold.com/2010/10/15/adding-info-windows-to-map-clusters-with-google-maps-api-v3/

I want to add the info of the markers (e.g. their 'title' s to a list displayed in the info window when clicked on the markercluster object.) contained in respective cluster.

So if I have 3 clusters at a particular zoom level in view, each having 5 markers insider it. How do I display a list of the titles of the 5 (out of total 15 markers in markercluster object) markers aggregated in that particular cluster?

for e.g. I have 3 markers inside a cluster then how do I show this in the info window? titlemarker1 titlemarker2 titlemarker3

edit: as seen here http://www.blogwave.de/wp-content/uploads/2009/05/marker_cluster.png all the different clusters are an instance of one markercluster object. So if we use the getmarkers routine of markercluster object as mentioned in one of the answers below then we get list of all markers.

What I want is for e.g. to get a list of only those 18 markers from the total markers in the cluster marked with 18, first from left.


回答1:


Unfortunately, the MarkerClusterer reference is a bit scant. After browsing through the source code, it looks like you need to call the getMarkers method of the cluster object passed in (contrary to what I had previously suggested, which was to call the method on the markerClusterer).

For example, using the tutorial you've linked to:

google.maps.event.addListener(markerClusterer, 'clusterclick', function(cluster) {
    var content = '';

    // Convert lat/long from cluster object to a usable MVCObject
    var info = new google.maps.MVCObject;
    info.set('position', cluster.center_);

    //----
    //Get markers
    var markers = cluster.getMarkers();

    var titles = "";
    //Get all the titles
    for(var i = 0; i < markers.length; i++) {
        titles += markers[i].getTitle() + "\n";
    }
    //----


    var infowindow = new google.maps.InfoWindow();
    infowindow.close();
    infowindow.setContent(titles); //set infowindow content to titles
    infowindow.open(map, info);

});

EDIT: Updated in response to question edit.



来源:https://stackoverflow.com/questions/7271636/how-to-find-the-markers-in-the-individual-clusters-in-a-marker-cluster-object-i

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!