首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >您可以在不删除事件侦听器的情况下将其禁用吗?

您可以在不删除事件侦听器的情况下将其禁用吗?
EN

Stack Overflow用户
提问于 2011-02-02 02:56:16
回答 1查看 3.9K关注 0票数 7

由于google maps v3不支持多边形或折线的开始或结束编辑,因此我正在尝试构建自己的多边形或折线。

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

代码语言:javascript
复制
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});
            }
        });
EN

回答 1

Stack Overflow用户

发布于 2011-03-10 02:27:43

您可以对全局变量执行几乎相同的操作,如下所示:(并设置disableListener =true以禁用它)

代码语言:javascript
复制
var disableListener = false;
google.maps.event.addListener(marker, 'click', function() {
    if (disableListener)
        return;
    if (!marker.index == 0)
        marker.setMap(null);
}
票数 9
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4866755

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档