javascript 如何在 google maps api v3 中的标记集群对象中找到各个集群中的标记?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7271636/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 23:32:48  来源:igfitidea点击:

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

javascriptgoogle-mapsgoogle-maps-markersmarkerclusterer

提问by NT3RP

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/

我按照此处的教程在任何缩放级别将信息窗口添加到标记集群的各个集群。 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?

因此,如果我有 3 个处于特定缩放级别的集群,每个集群内部都有 5 个标记。如何显示聚合在该特定集群中的 5 个(markercluster 对象中总共 15 个标记中的)标记的标题列表?

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

例如,我在一个集群中有 3 个标记,那么我如何在信息窗口中显示它?标题标记1 标题标记2 标题标记3

edit: as seen here http://www.blogwave.de/wp-content/uploads/2009/05/marker_cluster.pngall 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.

编辑:如这里所见 http://www.blogwave.de/wp-content/uploads/2009/05/marker_cluster.png所有不同的集群都是一个markercluster 对象的实例。因此,如果我们使用以下答案之一中提到的 markercluster 对象的 getmarkers 例程,那么我们将获得所有标记的列表。

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.

我想要的是例如从标记为 18 的集群中的总标记中获取仅那些 18 个标记的列表,从左开始。

回答by NT3RP

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

不幸的是,MarkerClusterer 参考有点少。浏览源代码后,貌似需要调用传入getMarkerscluster对象的方法(和我之前建议的相反,就是调用 上的方法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.

编辑:更新以响应问题编辑。