单击时禁用标记



我想在单击标记时禁用标记,以便消息不再重复。 http://code.google.com/apis/maps/documentation/v3/reference.html#Marker

我在另一个类似的问题中setClickable发现此方法,但它只会使标记消失,而不会禁用它。

<!DOCTYPE html>
<html>
  <head>
      #map {
        height: 50%;
      }
      /* Optional: Makes the sample page fill the window. */
      html, body {
        height: 100%;
        margin: 0;
        padding: 0;
      }
    </style>
  </head>
  <body>
    <div id="map"></div>
    <div id="box"></div>
    <script>
      function initMap() {
        var myLatlng = {lat: -25.363, lng: 131.044};
        var map = new google.maps.Map(document.getElementById('map'), {
          zoom: 4,
          center: myLatlng
        });
        var marker = new google.maps.Marker({
          position: myLatlng,
          map: map,
          title: 'Click to zoom'
        });

        marker.addListener('click', function() {
          var box = document.getElementById("box");
          var para = document.createElement("p");
          var node = document.createTextNode("Paragrap ")
          para.appendChild(node);
          box.appendChild(para);
        });
      }
    </script>
    <script async defer
    src="https://maps.googleapis.com/maps/api/js?key=AIzaSyB_nzqNDBfJopGdzH2sNBbgHS-iotxkN_c&callback=initMap">
    </script>
  </body>
</html>

https://codepen.io/anon/pen/ejLJOg

来自文档:

删除事件侦听
若要删除特定事件侦听器,必须已将其分配给变量。然后,您可以调用 removeListener((,传递分配给侦听器的变量名称。

var listener1 = marker.addListener('click', aFunction(;
google.maps.event.removeListener(listener1(;

删除事件侦听器回调函数中的侦听器:

var listener = marker.addListener('click', function() {
  var box = document.getElementById("box");
  var para = document.createElement("p");
  var node = document.createTextNode("Paragrap ")
  para.appendChild(node);
  box.appendChild(para);
  google.maps.event.removeListener(listener);
});

代码片段:

#map {
  height: 50%;
}
html,
body {
  height: 100%;
  margin: 0;
  padding: 0;
}
<div id="map"></div>
<div id="box"></div>
<script>
  function initMap() {
    var myLatlng = {
      lat: -25.363,
      lng: 131.044
    };
    var map = new google.maps.Map(document.getElementById('map'), {
      zoom: 4,
      center: myLatlng
    });
    var marker = new google.maps.Marker({
      position: myLatlng,
      map: map,
      title: 'Click to zoom'
    });
    var listener = marker.addListener('click', function() {
      var box = document.getElementById("box");
      var para = document.createElement("p");
      var node = document.createTextNode("Paragrap ")
      para.appendChild(node);
      box.appendChild(para);
      google.maps.event.removeListener(listener);
    });
  }
</script>
<script async defer src="https://maps.googleapis.com/maps/api/js?callback=initMap">
</script>

相关内容

  • 没有找到相关文章

最新更新