2

我尝试使用 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>
4

1 回答 1

1

好的,这是从谷歌地图中删除单个标记的简单技巧。

var icon = "icon.png";

                var marker = new google.maps.Marker({
                position: myLatlng,
                map: map,
                title: 'I am from Nepal.',
                icon: icon,
                });

如果您是 Photoshop 极客,请将您的图标大小设置为 1px ,降低不透明度以使其透明.. 它会像魅力一样工作.. 玩得开心..

于 2015-07-14T12:34:47.887 回答