我整理了一个独立页面,仅更改标记选项(未知引用)。关键部分似乎是调用删除的位置。它必须能够引用地图变量。map.deleteOverlays();
这是 JS Fiddle 和代码:
http://jsfiddle.net/VUfEy/
<!DOCTYPE html>
<html>
<head>
<style type="text/css">
html, body, #map_canvas { margin: 0; padding: 0; height: 100% }
</style>
<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false"></script>
<script type="text/javascript">
var map;
var markersArray = [];
var mapOptions = { center: new google.maps.LatLng(0.0, 0.0), zoom: 2,
mapTypeId: google.maps.MapTypeId.ROADMAP };
function initialize() {
map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);
google.maps.event.addListener(map, 'click', function(e) {
addMarker(e.latLng);
});
document.getElementById("delete").onclick = function() {
map.deleteOverlays();
};
}
function addMarker(latLng) {
var marker = new google.maps.Marker({
map: map,
//position: new google.maps.LatLng(lat, lng),
//title: description,
//icon: image,
//shadow: shadow,
//shape: shape
position: latLng
});
markersArray.push(marker);
}
google.maps.Map.prototype.deleteOverlays = function() {
if (markersArray.length) {
var i;
for (i in markersArray) {
markersArray[i].setMap(null);
}
markersArray.length = 0;
}
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
</head>
<body>
<button id="delete">delete overlays</button>
<div id="map_canvas"></div>
</body>
</html>