2011-02-01 11 views
7

Googleマップapi v3では、ポリゴンまたはポリラインの開始または終了の編集がサポートされていないため、自分自身を構築しようとしています。イベントリスナーを削除せずに無効にすることはできますか?

私は各ポイントのマーカーを描画してから編集を終了します。最初のインデックスポイント( "0")をクリックしてクリック可能にすると、すべてのマーカーを非表示に設定します。しかし、ユーザーはマップをクリックしてポリゴンの描画を続けることができます。私はそのイベントリスナーを無効にしたいが、マウスオーバーで再び有効にしたい。これはできますか? Remove 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 =を設定し、それを無効にする)

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

1を非常に素晴らしいソリューション –

関連する問題