簡體   English   中英

無法在GoogleMaps中刪除事件監聽器

[英]Cannot remove event listener in GoogleMaps

我在單擊按鈕時注冊了一個事件:

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

事件模塊定義了以下功能:

  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塊中的代碼?

我不確定requirejs模塊的工作方式...因為addEventToTarget沒有返回任何內容,所以我將更改以下內容:

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

然后在e.addEventToTargetOnce(map,'rightclick',function(event){...調用removeListener(mapPolylineListener)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM