1
votes

Why i'm unable to unbind event in openlayers 3? I'm trying to draw circle interactivily. For unbind event I'm using map.un('click',function(){ ... });

M.on('click',function(e){
      if(!$pec.hasClass('active')) { deactive(true); return false;}
            if( !isDrawing ){
                isDrawing = true;
                var center = e.coordinate;
                var circle = new ol.geom.Circle([center[0],center[1]],10000,'XY');
                var feature = new ol.Feature(circle);
                var vectorSource = new ol.source.Vector();
                vectorSource.addFeature(feature);
                c = new ol.layer.Vector({
                    source: vectorSource
                });
                c.circle=circle;
                M.addLayer(c);
                $('body').css('cursor','crosshair');
            }
            else {
                isDrawing = false;
                deactive(false);
                $('body').css('cursor','default');
                finishDraw('circle',c);
            }
        });
        M.on('pointermove',function(e){
            if(isDrawing){
                c.circle.setRadius(distanceTo(c.circle.getCenter(),e.coordinate)); 
            }
        }); 
        function deactive(all){
            M.un('click',function(){ log('deactive click');});
            M.un('pointermove',function(){});
            if(c && all) M.removeLayer(c);
            isDrawing = false;
        }

How to solve it ? I have more unbinds like this and all wont work -.-

1
I thought for jquery reverse of on() was off() no? - voldomazta
Yes. But for openlayers 3 is un(). openlayers.org/en/v3.1.1/apidoc/ol.Map.html#un - Skodsgn

1 Answers

3
votes

What I think happens here is that the method you bind your event with is anonymous. When you want to unbind an event, the same method has to be sent as a reference.

Try to declare you function as a variable, then use it in both your bind and unbind actions. Something like:

var myFunc = function(e) {};
M.on('click', myFunc);
M.un('click', myFunc)

Also, the function themselves are not called when unbounded. That is why your logging doesn't work.