Google 地图 V3 - 删除标记和更新集群的问题
首先,感谢为本网站做出贡献的所有人。我不是专家,但我从这个社区学到了很多东西:)
我有一个带有自定义菜单的谷歌地图,允许用户过滤特定区域中的类别,根据选择的类别向地图添加标记。当标记可见时,它们被添加到称为“标记”的数组中。一切似乎都工作正常,但后来我添加了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 似乎是一种可接受的方法,但在这种情况下这对我不起作用。
First off, thanks to everybody that contributes to this website. I am no expert, but I've learnt so much from this community :)
I have a google map with a custom menu that allows the user to filter categories in a particular area, adding markers to the map depending on what categories are selected. The markers are added to an array called 'markers' when visible. Every thing seemed to be working fine, but then I added the markercluster.js plugin and my problems began. When markers from a category are removed from the map, they don't get removed from the markers array. This seems to have a knock on effect to the clusters numbers as they do not update when markers are removed, only when they are added.
// 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();
});
},
Any help would be greatly appreciated. This is my first time using the google api so am a little lost and would love some advice or pointers. I've tried so many things including what seems to be an accepted method of setting the markers array to 0 but that does not work for me in this situation.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为这一定是因为变量标记范围不是全局的,所以删除脚本时无法访问标记变量
I think this must be because the variable marker scope is not global so when removing the script can not access the marker variable
我认为你的问题是你只是设置标记不显示
我认为你还需要添加一些逻辑来从标记数组中删除单个标记,如下所示:
这可能需要你在另一个中管理它们如果你想在内存中跟踪它们,可以使用数组。
或者,您可以管理 MarkerClusterer 的多个实例,每种类型一个。然后你可以调用
Bonus (opt_textColor & opt_anchor):
我相信你的变量应该只是“textColor”& "anchor":
...
I think your problem here is that you're simply setting the markers not to show
I think you need to also add some logic to remove the individual marker from the array of markers, like so:
This will probably require you to manage them in another array if you wish to keep track of them in memory.
Alternatively, you could manage multiple instances of MarkerClusterer, one for each type. Then you could just call
Bonus (opt_textColor & opt_anchor):
I believe your variables should just be "textColor" & "anchor":
...