我尝试使用 clearoverlays() 删除谷歌地图标记,但我无法解决这个问题.. 我想要的只是从我的地图中删除单个标记。但我只能删除所有标记。我的代码使用数组..这是我的代码
<!DOCTYPE html>
<html>
<head>
<script
src="http://maps.googleapis.com/maps/api/js?key=AIzaSyDY0kkJiTPVd2U7aTOAwhc9ySH6oHxOIYM&sensor=false">
</script>
<script>
var map;
var myCenter=new google.maps.LatLng(51.508742,-0.120850);
var markers = [];
function initialize()
{
var mapProp = {
center:myCenter,
zoom:5,
mapTypeId:google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById("googleMap"),mapProp);
google.maps.event.addListener(map, 'click', function(e) {
placeMarker(e.latLng);
});
google.maps.event.addListener(map, 'rightclick', function(){
clearOverlays();
});
}
function placeMarker(e) {
var marker = new google.maps.Marker({
position: e,
map: map,
});
var infowindow = new google.maps.InfoWindow({
content: 'Latitude: ' + e.lat() + '<br>Longitude: ' + e.lng()
});
infowindow.open(map,marker);
markers.push(marker);
}
function setAllMap(map) {
for (var i = 0; i < markers.length; i++) {
markers[i].setMap(map);
}
}
// Removes the overlays from the map, but keeps them in the array.
function clearOverlays() {
for (var i = markers.length; i > 0; i--){
markers[markers.length-i].setMap(null);
}
}
// Shows any overlays currently in the array.
function showOverlays() {
setAllMap(map);
}
// Deletes all markers in the array by removing references to them.
function deleteOverlays() {
clearOverlays();
markers = [];
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
</head>
<body>
<div id="googleMap" style="width:500px;height:380px;"></div>
<input onclick="clearOverlays();" type=button value="Hide Overlays">
<input onclick="showOverlays();" type=button value="Show All Overlays">
<input onclick="deleteOverlays();" type=button value="Delete Overlays">
<input onclick="deleteMarker(marker);" type=button value="delete mark">
</body>
</html>