你可以禁用事件监听器而不删除它吗?

时间:2011-02-01 18:56:16

标签: google-maps google-maps-api-3

由于谷歌地图api v3不支持多边形或折线的开始或结束编辑,我正在尝试构建我自己的一个。

我正在为每个点绘制标记,然后完成编辑我在单击第一个索引点(“0”)时将所有标记设置为隐藏,然后将多边形设置为clickable为true。但是用户仍然可以单击地图并继续绘制多边形。我想禁用该事件监听器,但在鼠标悬停时重新启用它。可以这样做吗?如果我使用Remove Listener,我可以在鼠标悬停时将另一个监听器重新连接到多边形,以便他们可以编辑吗?

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});
            }
        });

1 个答案:

答案 0 :(得分:9)

你可以用全局变量做同样的事情,如下所示: (并设置disableListener = true;禁用它)

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