我已經創建了一個標記數組。我使用這些標記陣列來聆聽'點擊'並將標記放置在Google地圖上,並創建函數以「清除所有標記」,「重新顯示所有標記」和「刪除所有標記」。如何在谷歌地圖上一次刪除一個標記
問題是,我如何以一種能夠清除或刪除一次一個標記的方式執行此操作?原因是因爲如果我碰巧無意中劃到了一個我不想要的地方,而且我想清除/刪除它,我無法做到這一點。如果我是清除/刪除特定的標記,那我以前繪製的將被清除標記其餘/刪除,以及...
我的代碼:
//Initialize the map
function initialize() {
var myLatlng = new google.maps.LatLng(2,110);
var myOptions = {
zoom: 3,
center: myLatlng,
mapTypeId: google.maps.MapTypeId.HYBRID
};
map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
infowindow = new google.maps.InfoWindow({
content: "loading..."
});
}
function changeForm(the_form) {
window.location = the_form;
}
//Listen for click
function marker() {
google.maps.event.addListener(map, 'click', function(event) {
addMarker(event.latLng);
});
}
// Place markers in by click
function addMarker(location) {
marker = new google.maps.Marker({
position: location,
map: map,
title:"Specified Location",
icon: 'images/greenPoint.png'
});
markersArray.push(marker);
}
// Deletes all markers in the array by removing references to them
function deleteOverlays() {
if (markersArray) {
for (i in markersArray) {
markersArray[i].setMap(null);
}
markersArray.length = 0;
}
}
// Removes the overlays from the map, but keeps them in the array
function clearOverlays() {
if (markersArray) {
for (i in markersArray) {
markersArray[i].setMap(null);
}
}
}
// Shows any overlays currently in the array
function showOverlays() {
if (markersArray) {
for (i in markersArray) {
markersArray[i].setMap(map);
}
}
}