unbind doesn't work

Skodsgn

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 -.-

Alexandre Dubé

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.

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사