我已经创建了一个标记数组。我使用这个标记数组来监听'click'事件,并在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);
}
}
}