简体   繁体   English

无法在GoogleMaps中删除事件监听器

[英]Cannot remove event listener in GoogleMaps

I have an event that I register on a button click: 我在单击按钮时注册了一个事件:

 var paths=[];
 var mapPolylineListener=e.addEventToTarget(map,'click',function(event){
    //keep adding points to the array
    //draw the polyline if there are two points
    //extend the polyline with this point once it has been drawn 
 }

 e.addEventToTargetOnce(map,'rightclick',function(event){
    console.log('Polyline right click event called');
    if(typeof mapPolylineListener!='undefined')
    {
      console.log('stop drawing the map listener');
          //do stuff here like encoding to geometry and making AJAX calls
      paths=[];
      mapPolylineListener=void 0;
    }               

The events module defines the functions: 事件模块定义了以下功能:

  addEventToTarget:function(onWhat,eventType,fn)
  {
     gmaps.event.addListener(onWhat,eventType,fn);
  }

  addEventToTargetOnce:function(onWhat,eventType,fn)
  {
    gmaps.event.addListenerOnce(onWhat,eventType,fn);
  }

I am using requirejs modules and I used modules to specify functionality: 我正在使用requirejs模块,并使用模块来指定功能:

 e=gmaps.event;
 gmaps=window.google.maps;

In my console,it displays: 在我的控制台中,它显示:

  Polyline right click event called

and the check always seems to fail 而且检查似乎总是失败

 if(typeof mapPolylineListener!='undefined')

So,I have tried doing a !=null and if(mapPolylineListener) check both of which failed. 所以,我尝试做一个!=nullif(mapPolylineListener)检查两者都失败了。

How can I fix this so that the code within the if block is executed? 如何解决此问题,以便执行if块中的代码?

I am not sure how requirejs module works... Because nothing is returned from addEventToTarget I would change the following: 我不确定requirejs模块的工作方式...因为addEventToTarget没有返回任何内容,所以我将更改以下内容:

addEventToTarget:function(onWhat,eventType,fn)
{
    var handle = gmaps.event.addListener(onWhat,eventType,fn);
    return handle;
}

and then call removeListener(mapPolylineListener) in e.addEventToTargetOnce(map,'rightclick',function(event){... 然后在e.addEventToTargetOnce(map,'rightclick',function(event){...调用removeListener(mapPolylineListener)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM