2011-02-01 14 views
7

由於谷歌地圖api v3不支持多邊形或多段線的開始或結束編輯,我正試圖構建自己的一個。您可以禁用事件列表程序而不刪除它嗎?

我正在爲每個點繪製標記,然後完成編輯我設置所有標記以隱藏單擊第一個索引點(「0」)時將多邊形設置爲可點擊爲true。但用戶仍然可以單擊地圖並繼續繪製多邊形。我想禁用該事件列表器,但重新啓用它在鼠標懸停..可以這樣做嗎?如果我使用Remove Listner,我可以在mouseover上重新連接另一個listner到多邊形,以便它們可以編輯它?

MapShaper.Feature.prototype.poly = function(type) { 
    var color = MapShaper.getColor(false), 
    path = new google.maps.MVCArray, 
    poly, 
    self = this, 
    el = type + "_b"; 

    if(type=="shape"){ 
     poly = self.createShape({strokeWeight: 3, fillColor: color}, path); 
    }else if(type=="line"){ 
     poly = self.createLine({strokeWeight: 3, strokeColor: color }, path); 
    } 

    poly.markers = new google.maps.MVCArray; 

    google.maps.event.addListener(poly, "mouseover", function(){  
     poly.markers.forEach(function(polyMarker, index){ 
      polyMarker.setVisible(true); 
     }); 
    }); 

MapShaper.Feature.prototype.createShape = function(opts, path) { 
    var poly; 
    poly = new google.maps.Polygon({ 
     clickable:false, 
     strokeWeight: opts.strokeWeight, 
     fillColor: opts.fillColor 
    }); 
    poly.setPaths(new google.maps.MVCArray([path])); 
    return poly; 
} 

MapShaper.Feature.prototype.createShape = function(opts, path) { 
    var poly; 
    poly = new google.maps.Polygon({ 
     clickable:false, 
     strokeWeight: opts.strokeWeight, 
     fillColor: opts.fillColor 
    }); 
    poly.setPaths(new google.maps.MVCArray([path])); 
    return poly; 
} 


     google.maps.event.addListener(marker, 'click', function() { 
      if (!marker.index == 0) { 
       marker.setMap(null); 
       markers.removeAt(marker.index); 
       path.removeAt(marker.index); 
       MapShaper.reindex(markers);    
       if(markers.getLength() == 0){ 
        MapShaper.removeFeature(poly.id); 
       } 
      } else { 
       markers.forEach(function(marker, index) { 
        marker.setVisible(false) 
       }); 
       poly.setOptions({clickable: true}); 
      } 
     }); 

回答

9

你可以做幾乎同樣的事情用一個全局變量,像這樣: (並設置disableListener = TRUE;要禁用)

var disableListener = false; 
google.maps.event.addListener(marker, 'click', function() { 
    if (disableListener) 
     return; 
    if (!marker.index == 0) 
     marker.setMap(null); 
} 
+0

+1非常好的解決方案 – 2014-09-09 10:48:12

相關問題