首先,感謝大家有助於這個網站。我不是專家,但我已經學會從這個社區:)谷歌地圖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();
});
},
任何幫助將不勝感激。這是我第一次使用谷歌API,所以我有點失落,並會喜歡一些建議或指針。我已經嘗試了很多東西,包括將標記數組設置爲0的可接受方法,但在這種情況下對我無效。