如何在单击后禁用标记上的单击事件

时间:2016-06-29 10:27:34

标签: javascript php ajax

点击一个标记后,我想:

  1. 在地图上停用点击事件
  2. 等待5秒
  3. 在地图上重新启用点击事件 我使用ajax和setInterval 3秒钟 这是我的代码:

    details = new google.maps.LatLng(18.60301515,73.79623622); bindInfoWindow(marker,map,infoWindow,details);

    function bindInfoWindow(marker,map,infoWindow,description){

    google.maps.event.addListener(marker,'click', function() {
        var geocoder =  new google.maps.Geocoder();
    
        geocoder.geocode({ 'latLng': description }, function (results, status) {
            if (status == google.maps.GeocoderStatus.OK) {
                if (results[1]) {
                    var location1=results[1].formatted_address;
                    // set content to marker at click event
                    infoWindow.setContent('Location:'+location1+'<br>');
                }
            }
        });
        infoWindow.open(map, marker);
    });
    

    }

  4. 由于

2 个答案:

答案 0 :(得分:2)

我会做这样的事情:

的JavaScript

details = new google.maps.LatLng(18.60301515,73.79623622);
bindInfoWindow(marker, map, infoWindow , details);  
var allowClick = true;

function bindInfoWindow(marker, map, infoWindow, description) {
  google.maps.event.addListener(marker,'click', function() {
    if(allowClick) {
      allowClick = false;

      var geocoder =  new google.maps.Geocoder();
      geocoder.geocode({ 'latLng': description }, function (results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
          if (results[1]) {
            var location1=results[1].formatted_address;    
            infoWindow.setContent('Location:'+location1+'<br>');
          }
        }
      });
      infoWindow.open(map, marker);

      setTimeout(function() { allowClick = true; }, 5000);
    }
  });
} 

答案 1 :(得分:1)

您可以使用标志(全局值)。和Settimer在5秒后改变标志的值。

details = new google.maps.LatLng(18.60301515,73.79623622);
bindInfoWindow(marker, map, infoWindow , details);  
 var flag_timer=true;
function bindInfoWindow(marker, map, infoWindow, description) {

      google.maps.event.addListener(marker,'click', function() {
         if(flag_timer===true){
         var geocoder =  new google.maps.Geocoder();

         geocoder.geocode({ 'latLng': description }, function (results, status) {
            if (status == google.maps.GeocoderStatus.OK) {
                if (results[1]) {
                   var location1=results[1].formatted_address;    
         infoWindow.setContent('Location:'+location1+'<br>');      // set content to marker at click event


                }
            }
        });
        infoWindow.open(map, marker);
         flag_timer=false;
        setTimeout(function() { flag_timer = true; }, 5000);
     }
  });


}