Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/77.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Google Maps V3-删除标记和更新群集的问题_Javascript_Jquery_Arrays_Google Maps Api 3_Markerclusterer - Fatal编程技术网

Javascript Google Maps V3-删除标记和更新群集的问题

Javascript Google Maps V3-删除标记和更新群集的问题,javascript,jquery,arrays,google-maps-api-3,markerclusterer,Javascript,Jquery,Arrays,Google Maps Api 3,Markerclusterer,首先,感谢所有为本网站做出贡献的人。我不是专家,但我从这个社区学到了很多东西: 我有一个带有自定义菜单的谷歌地图,允许用户过滤特定区域的类别,根据选择的类别向地图添加标记。标记在可见时添加到称为“标记”的数组中。一切似乎都很好,但后来我添加了markercluster.js插件,我的问题就开始了。当类别中的标记从地图中删除时,它们不会从标记数组中删除。这似乎对簇号有连锁反应,因为移除标记时簇号不会更新,只有添加标记时簇号才会更新 // The navigaiton item th

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

我有一个带有自定义菜单的谷歌地图,允许用户过滤特定区域的类别,根据选择的类别向地图添加标记。标记在可见时添加到称为“标记”的数组中。一切似乎都很好,但后来我添加了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();

        });

    },

任何帮助都将不胜感激。这是我第一次使用谷歌api,所以我有点不知所措,希望能得到一些建议或指点。我已经尝试了很多方法,包括将markers数组设置为0的公认方法,但在这种情况下这对我不起作用。

我认为这一定是因为变量marker作用域不是全局的,所以在删除脚本时无法访问marker变量。

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

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();
附加选项文本颜色和选项锚定: 我相信你的变量应该是textColor&anchor:

“样式”:对象具有样式属性的对象: ... “锚定”:数组标签文本的锚定位置。 “textColor”:字符串文本颜色。