开发者

can you disable an event listener without removing it?

开发者 https://www.devze.com 2023-02-07 19:13 出处:网络
Since google maps api v3 doesn\'t support start or end editing of a polygon or polyline I am trying to construct one of my own.

Since google maps api v3 doesn't support start or end editing of a polygon or polyline I am trying to construct one of my own.

I am drawing markers for each point, then to finish editing I set all marker to hide when the first index point ("0") is clicked then set the polygon to clickable is true. But the user can still click the map and continue drawing the polygon. I want to disable that event listener but re-enable it on mouse over.. Can this be done? If I use Remove Listener can I reattached another listener to the polygon on mouseover so they can edit it?

MapShaper.Feature.prototype.poly = function(type) {
    var color = MapShaper.getCo开发者_如何学JAVAlor(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});
            }
        });


You can do pretty much the same thing with a global variable, like so: (and set disableListener = true; to disable it)

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

精彩评论

暂无评论...
验证码 换一张
取 消