简体   繁体   中英

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:

 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.

How can I fix this so that the code within the if block is executed?

I am not sure how requirejs module works... Because nothing is returned from addEventToTarget I would change the following:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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