Вы можете отключить прослушиватель событий, не удаляя его? - PullRequest
7 голосов
/ 01 февраля 2011

Поскольку Google Maps API v3 не поддерживает начало или конец редактирования многоугольника или полилинии, я пытаюсь создать один из моих собственных.

Я рисую маркеры для каждой точки, затем, чтобы закончить редактирование, я устанавливаю все маркеры, чтобы скрыть их при нажатии первой индексной точки («0»), а затем устанавливаю полигон «кликабельный». Но пользователь все еще может щелкнуть карту и продолжить рисование многоугольника. Я хочу отключить прослушиватель событий, но снова включить его при наведении мыши. Можно ли это сделать? Если я использую 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 Ответ

9 голосов
/ 09 марта 2011

Вы можете сделать почти то же самое с глобальной переменной, например так: (и установите disableListener = true; чтобы отключить его)

var disableListener = false;
google.maps.event.addListener(marker, 'click', function() {
    if (disableListener)
        return;
    if (!marker.index == 0)
        marker.setMap(null);
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...