无法在Google地图中删除事件侦听器

时间:2014-04-04 10:09:33

标签: javascript events google-maps-api-3

我有一个事件,我在点击按钮上注册:

 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;
    }               

events模块定义了函数:

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

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

我正在使用requirejs模块,我使用模块来指定功能:

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

在我的控制台中,它会显示:

  Polyline right click event called

并且检查似乎总是失败

 if(typeof mapPolylineListener!='undefined')

所以,我尝试过!=nullif(mapPolylineListener)检查两者都失败了。

如何解决这个问题,以便执行if块中的代码?

1 个答案:

答案 0 :(得分:1)

我不确定requirejs模块是如何工作的......因为addEventToTarget没有返回任何内容我会改变以下内容:

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

然后在removeListener(mapPolylineListener)

中致电e.addEventToTargetOnce(map,'rightclick',function(event){...