【问题标题】:Google Maps V3 - Issue with removing markers and updating clustersGoogle Maps V3 - 删除标记和更新集群的问题
【发布时间】:2012-03-13 19:22:11
【问题描述】:

首先,感谢为本网站做出贡献的所有人。我不是专家,但我从这个社区学到了很多东西:)

我有一个带有自定义菜单的谷歌地图,允许用户过滤特定区域中的类别,根据选择的类别向地图添加标记。标记在可见时被添加到称为“标记”的数组中。一切似乎都运行良好,但后来我添加了 markercluster.js 插件,我的问题开始了。当从地图中删除某个类别的标记时,它们不会从标记数组中删除。这似乎对集群数量产生了连锁反应,因为它们不会在删除标记时更新,只有在添加它们时才会更新。

        // The navigaiton item that has been clicked 
        $(source + ' .map-controls ul li a').live('click', function(e){
            // The category of the clicked item eg. hikes, kayaking etc
            var markerClass = $(this).attr('class');
            // If the clicked items is not visible on the map
            if(!$(this).parent().hasClass('visible')) {
                // Go through JSON to find matching categories
                for(i=0; i<category.length; i++) {
                    // If we find a match to the category of the clicked item
                    if(category[i].mapmarker == markerClass){
                        // Grab the latitude and longitude
                        var lat = category[i].lat;
                        var long = category[i].long;
                        // Create a position variable
                        var myLatlng = new google.maps.LatLng(lat,long);
                        latslongs.push(myLatlng);
                        // Create the marker
                        var marker = new google.maps.Marker({
                            position: myLatlng,
                            map: map
                        });
                        // Give the marker and id of the category clicked
                        marker.setValues({type: "point", id: markerClass});
                        // Set the category to the category clicked
                        marker.mycategory = markerClass;
                        // Push the marker to the markers array
                        markers.push(marker);           
                    }
                }
                // Marker Cluster options - Bonus point if you can tell me why opt_textColor and opt_anchor don't work?
                mcOptions = {styles: [{
                    opt_textColor: 'white',
                    height: 47,
                    url: "http://localhost/img/bgs/bg-marker-cluster.png",
                    width: 46,
                    opt_anchor: [5, 12]
                }]}
                // Set up MarkerCluster
                var markerCluster = new MarkerClusterer(map, markers, mcOptions);
                // Make the markers visible on the map
                $(this).parent().addClass('visible');

            } else {
                    // ELSE - If the clicked categories markers are visible when clicked, we go through the array of all the markers currently visible on the map
                    for (i=0; i < markers.length; i++) {
                        // If we find a match to the clicked category
                        if(markers[i].get('id') == markerClass){
                            // HERE IS WHERE I HAVE MY PROBLEM!!
                            // This line works as in it removes the correct markers from the map, but not from the markers array.
                            // I've seen suggestions of markers.lenght = 0 but I can't do that as I have others markers on the map
                            markers[i].setMap(null);
                            // I only found this out when adding the cluster plugin. Before it seemed to be working perfectly but 
                            // I had no idea the array just kept growing in size while showing/hiding different categories.
                            // I have tried so many things to try and remove the correct array items from the array, but I can't get it.
                            // The other issue is that the cluster numbers are not updating when removing categories??? But they get higher
                            // everytime you add a category. This might be a knock on effect of the ever growing markers array.
                        }
                    }
                    // Make the markers invisible on the map
                    $(this).parent().removeClass('visible');

            }
            // Thanks guys and girls.
            e.preventDefault();

        });

    },

任何帮助将不胜感激。这是我第一次使用 google api,所以有点迷茫,希望得到一些建议或指示。我已经尝试了很多东西,包括似乎可以接受的将标记数组设置为 0 的方法,但在这种情况下这对我不起作用。

【问题讨论】:

    标签: javascript jquery arrays google-maps-api-3 markerclusterer


    【解决方案1】:

    我认为您的问题是您只是将标记设置为不显示

    markers[i].setMap(null);
    

    我认为您还需要添加一些逻辑来从标记数组中删除单个标记,如下所示:

    //remove the marker from the map
    markers[i].setMap(null);
    //remove the marker from the array
    markers.splice(i, 1);
    

    如果您希望在内存中跟踪它们,这可能需要您在另一个数组中管理它们。

    或者,您可以管理多个 MarkerClusterer 实例,每种类型一个。然后你可以打电话

    markerCluster.clearMarkers();
    

    奖金(opt_textColor 和 opt_anchor): 我相信你的变量应该只是“textColor”和“anchor”:

    • 'styles':(对象)具有样式属性的对象: ...
      • 'anchor':(数组)标签文本的锚点位置。
      • 'textColor':(字符串)文本颜色。

    【讨论】:

      【解决方案2】:

      我认为这一定是因为变量标记范围不是全局的所以在删除脚本时无法访问标记变量

      【讨论】:

        猜你喜欢
        • 2011-05-14
        • 2015-07-16
        • 2013-12-28
        • 2012-07-20
        • 2012-05-24
        • 2013-05-02
        • 1970-01-01
        • 2011-09-05
        • 2011-10-07
        相关资源
        最近更新 更多