如何在Google地图中隐藏和显示MarkerClusterer [英] How to hide and show MarkerClusterer in google maps

查看:159
本文介绍了如何在Google地图中隐藏和显示MarkerClusterer的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当用户单击某些按钮时,我正在尝试隐藏/显示markerClusterer:

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

这就是我想要做的:

    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]);
    }

我创建了多个markerClusterer,每个都与一个特定名称相关联.

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!

推荐答案

我整个上午都在为此苦苦挣扎,但幸运的是我找到了解决方法.

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

首先,请确保您具有最新的MarkerClustererPlus版本 https://github.com/googlemaps/js-marker-clusterer

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

那么这很容易

创建标记时,请确保您

将其可见标志设置为false.

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();

隐藏集群:

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

markerCluster.repaint();

希望能有所帮助

这篇关于如何在Google地图中隐藏和显示MarkerClusterer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆