首先,感谢所有为本网站做出贡献的人。我不是专家,但我从这个社区学到了很多东西:)
我有一个带有自定义菜单的谷歌地图,该菜单允许用户过滤特定区域中的类别,根据选择的类别向地图添加标记。标记在可见时添加到名为“标记”的数组中。似乎每件事情都很好,但后来我添加了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,所以我有点失落,并会喜欢一些建议或指针。我已经尝试了很多东西,包括将标记数组设置为0的可接受方法,但在这种情况下这对我不起作用。
答案 0 :(得分:0)
我认为这一定是因为变量标记范围不是全局的,因此当删除脚本时无法访问标记变量
答案 1 :(得分:0)
我认为你的问题在于你只是将标记设置为不显示
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&amp; opt_anchor): 我相信你的变量应该只是“textColor”&amp; “锚”: