How to delete a listener in OpenLayers 3









up vote
5
down vote

favorite
1












I make a copy of my question here at stackoverflow, because at gis.stackexchange all my questions do not attract any attention - many times I could not get an answer to simple questions there. So, my question now is how to delete a listener defined this way:



map.getViewport().addEventListener('click', function (e)
console.log("clicked");
);









share|improve this question























  • if you use getViewport() it will return a HTMLElement which has nothing to do with openlayers. See developer.mozilla.org/en-US/docs/Web/API/HTMLElement
    – Simon Zyx
    Jan 26 '16 at 14:23










  • openlayers listener adding and removing is described at openlayers.org/en/master/apidoc/ol.Object.html, btw.
    – Simon Zyx
    Jan 26 '16 at 14:24






  • 1




    If you want to register a click on the map to, let's say, know the coordinate clicked, then you definitively should use the ol3 native map browser events as the event object would contain that information.
    – Alexandre Dubé
    Jan 26 '16 at 15:33










  • @Alexandre Dubé. Thanks! Now I'm using map.on
    – Jacobian
    Jan 26 '16 at 15:34










  • Can you go back and accept @Alexandre Dubé answer, as it seems to be the most helpful.
    – mcneela86
    Feb 15 at 16:36














up vote
5
down vote

favorite
1












I make a copy of my question here at stackoverflow, because at gis.stackexchange all my questions do not attract any attention - many times I could not get an answer to simple questions there. So, my question now is how to delete a listener defined this way:



map.getViewport().addEventListener('click', function (e)
console.log("clicked");
);









share|improve this question























  • if you use getViewport() it will return a HTMLElement which has nothing to do with openlayers. See developer.mozilla.org/en-US/docs/Web/API/HTMLElement
    – Simon Zyx
    Jan 26 '16 at 14:23










  • openlayers listener adding and removing is described at openlayers.org/en/master/apidoc/ol.Object.html, btw.
    – Simon Zyx
    Jan 26 '16 at 14:24






  • 1




    If you want to register a click on the map to, let's say, know the coordinate clicked, then you definitively should use the ol3 native map browser events as the event object would contain that information.
    – Alexandre Dubé
    Jan 26 '16 at 15:33










  • @Alexandre Dubé. Thanks! Now I'm using map.on
    – Jacobian
    Jan 26 '16 at 15:34










  • Can you go back and accept @Alexandre Dubé answer, as it seems to be the most helpful.
    – mcneela86
    Feb 15 at 16:36












up vote
5
down vote

favorite
1









up vote
5
down vote

favorite
1






1





I make a copy of my question here at stackoverflow, because at gis.stackexchange all my questions do not attract any attention - many times I could not get an answer to simple questions there. So, my question now is how to delete a listener defined this way:



map.getViewport().addEventListener('click', function (e)
console.log("clicked");
);









share|improve this question















I make a copy of my question here at stackoverflow, because at gis.stackexchange all my questions do not attract any attention - many times I could not get an answer to simple questions there. So, my question now is how to delete a listener defined this way:



map.getViewport().addEventListener('click', function (e)
console.log("clicked");
);






javascript openlayers-3






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Apr 13 '17 at 12:33









Community

11




11










asked Jan 26 '16 at 13:41









Jacobian

1,890947113




1,890947113











  • if you use getViewport() it will return a HTMLElement which has nothing to do with openlayers. See developer.mozilla.org/en-US/docs/Web/API/HTMLElement
    – Simon Zyx
    Jan 26 '16 at 14:23










  • openlayers listener adding and removing is described at openlayers.org/en/master/apidoc/ol.Object.html, btw.
    – Simon Zyx
    Jan 26 '16 at 14:24






  • 1




    If you want to register a click on the map to, let's say, know the coordinate clicked, then you definitively should use the ol3 native map browser events as the event object would contain that information.
    – Alexandre Dubé
    Jan 26 '16 at 15:33










  • @Alexandre Dubé. Thanks! Now I'm using map.on
    – Jacobian
    Jan 26 '16 at 15:34










  • Can you go back and accept @Alexandre Dubé answer, as it seems to be the most helpful.
    – mcneela86
    Feb 15 at 16:36
















  • if you use getViewport() it will return a HTMLElement which has nothing to do with openlayers. See developer.mozilla.org/en-US/docs/Web/API/HTMLElement
    – Simon Zyx
    Jan 26 '16 at 14:23










  • openlayers listener adding and removing is described at openlayers.org/en/master/apidoc/ol.Object.html, btw.
    – Simon Zyx
    Jan 26 '16 at 14:24






  • 1




    If you want to register a click on the map to, let's say, know the coordinate clicked, then you definitively should use the ol3 native map browser events as the event object would contain that information.
    – Alexandre Dubé
    Jan 26 '16 at 15:33










  • @Alexandre Dubé. Thanks! Now I'm using map.on
    – Jacobian
    Jan 26 '16 at 15:34










  • Can you go back and accept @Alexandre Dubé answer, as it seems to be the most helpful.
    – mcneela86
    Feb 15 at 16:36















if you use getViewport() it will return a HTMLElement which has nothing to do with openlayers. See developer.mozilla.org/en-US/docs/Web/API/HTMLElement
– Simon Zyx
Jan 26 '16 at 14:23




if you use getViewport() it will return a HTMLElement which has nothing to do with openlayers. See developer.mozilla.org/en-US/docs/Web/API/HTMLElement
– Simon Zyx
Jan 26 '16 at 14:23












openlayers listener adding and removing is described at openlayers.org/en/master/apidoc/ol.Object.html, btw.
– Simon Zyx
Jan 26 '16 at 14:24




openlayers listener adding and removing is described at openlayers.org/en/master/apidoc/ol.Object.html, btw.
– Simon Zyx
Jan 26 '16 at 14:24




1




1




If you want to register a click on the map to, let's say, know the coordinate clicked, then you definitively should use the ol3 native map browser events as the event object would contain that information.
– Alexandre Dubé
Jan 26 '16 at 15:33




If you want to register a click on the map to, let's say, know the coordinate clicked, then you definitively should use the ol3 native map browser events as the event object would contain that information.
– Alexandre Dubé
Jan 26 '16 at 15:33












@Alexandre Dubé. Thanks! Now I'm using map.on
– Jacobian
Jan 26 '16 at 15:34




@Alexandre Dubé. Thanks! Now I'm using map.on
– Jacobian
Jan 26 '16 at 15:34












Can you go back and accept @Alexandre Dubé answer, as it seems to be the most helpful.
– mcneela86
Feb 15 at 16:36




Can you go back and accept @Alexandre Dubé answer, as it seems to be the most helpful.
– mcneela86
Feb 15 at 16:36












2 Answers
2






active

oldest

votes

















up vote
13
down vote



accepted










OL3 emmits its own kind of events you could use instead and, to answer your original question, gives an easy and quick way to unregister them.



Look at this example: http://openlayers.org/en/v3.13.0/examples/vector-layer.html



More specifically, at these lines:



 map.on('pointermove', function(evt) 
if (evt.dragging)
return;

var pixel = map.getEventPixel(evt.originalEvent);
displayFeatureInfo(pixel);
);

map.on('click', function(evt)
displayFeatureInfo(evt.pixel);
);


The ol.Map object has a on method you can use to register event listeners on the ol3 map browser events. It's best to use those events instead of standard browser events. See the list of all map browser events here: http://openlayers.org/en/v3.13.0/apidoc/ol.MapBrowserEvent.html



To unregister, you can:



a) use the un method, but make sure you to give the same callback method as 2nd argument. In other words:



 var callback = function(evt) 
displayFeatureInfo(evt.pixel);
;
map.on('click', callback);
map.un('click', callback);


b) an other way is to use the ol.Observable.unByKey method, which I like a lot. When calling on or once, it returns a key that references the event. You can then use that key to unlisten your event:



 var key = map.on('click', function(evt) 
displayFeatureInfo(evt.pixel);
);
ol.Observable.unByKey(key);


I find b) to be more friendly as you can register a bunch of event listeners and put all the keys inside an array. When you want to unregister them all, loop in the array and call the unByKey method, then empty the array. It generates less code that way than having to unregister all events manually.






share|improve this answer





























    up vote
    1
    down vote













    to remove first thing to do is to declare an external function attached to the click
    and then remove it with removeEventListener



    like this



    function myFunction(event)
    //some stuff
    console.log("clicked");


    map.getViewport().addEventListener('click',myFunction);


    and then remove it by



    map.getViewport().removeEventListener('click',myFunction);


    that's all



    Here is a snippet






    function myfunction(e) 
    alert("event click");



    document.getElementById("mybtn").addEventListener("click",myfunction);



    document.getElementById("mybtn2").addEventListener("click",function(e)
    document.getElementById("mybtn").removeEventListener("click",myfunction);
    );

    <button id="mybtn">btn</button><br/>
    <button id="mybtn2" >remove btn click</button>








    share|improve this answer






















      Your Answer






      StackExchange.ifUsing("editor", function ()
      StackExchange.using("externalEditor", function ()
      StackExchange.using("snippets", function ()
      StackExchange.snippets.init();
      );
      );
      , "code-snippets");

      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "1"
      ;
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function()
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled)
      StackExchange.using("snippets", function()
      createEditor();
      );

      else
      createEditor();

      );

      function createEditor()
      StackExchange.prepareEditor(
      heartbeatType: 'answer',
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader:
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      ,
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );













       

      draft saved


      draft discarded


















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f35015174%2fhow-to-delete-a-listener-in-openlayers-3%23new-answer', 'question_page');

      );

      Post as a guest






























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      13
      down vote



      accepted










      OL3 emmits its own kind of events you could use instead and, to answer your original question, gives an easy and quick way to unregister them.



      Look at this example: http://openlayers.org/en/v3.13.0/examples/vector-layer.html



      More specifically, at these lines:



       map.on('pointermove', function(evt) 
      if (evt.dragging)
      return;

      var pixel = map.getEventPixel(evt.originalEvent);
      displayFeatureInfo(pixel);
      );

      map.on('click', function(evt)
      displayFeatureInfo(evt.pixel);
      );


      The ol.Map object has a on method you can use to register event listeners on the ol3 map browser events. It's best to use those events instead of standard browser events. See the list of all map browser events here: http://openlayers.org/en/v3.13.0/apidoc/ol.MapBrowserEvent.html



      To unregister, you can:



      a) use the un method, but make sure you to give the same callback method as 2nd argument. In other words:



       var callback = function(evt) 
      displayFeatureInfo(evt.pixel);
      ;
      map.on('click', callback);
      map.un('click', callback);


      b) an other way is to use the ol.Observable.unByKey method, which I like a lot. When calling on or once, it returns a key that references the event. You can then use that key to unlisten your event:



       var key = map.on('click', function(evt) 
      displayFeatureInfo(evt.pixel);
      );
      ol.Observable.unByKey(key);


      I find b) to be more friendly as you can register a bunch of event listeners and put all the keys inside an array. When you want to unregister them all, loop in the array and call the unByKey method, then empty the array. It generates less code that way than having to unregister all events manually.






      share|improve this answer


























        up vote
        13
        down vote



        accepted










        OL3 emmits its own kind of events you could use instead and, to answer your original question, gives an easy and quick way to unregister them.



        Look at this example: http://openlayers.org/en/v3.13.0/examples/vector-layer.html



        More specifically, at these lines:



         map.on('pointermove', function(evt) 
        if (evt.dragging)
        return;

        var pixel = map.getEventPixel(evt.originalEvent);
        displayFeatureInfo(pixel);
        );

        map.on('click', function(evt)
        displayFeatureInfo(evt.pixel);
        );


        The ol.Map object has a on method you can use to register event listeners on the ol3 map browser events. It's best to use those events instead of standard browser events. See the list of all map browser events here: http://openlayers.org/en/v3.13.0/apidoc/ol.MapBrowserEvent.html



        To unregister, you can:



        a) use the un method, but make sure you to give the same callback method as 2nd argument. In other words:



         var callback = function(evt) 
        displayFeatureInfo(evt.pixel);
        ;
        map.on('click', callback);
        map.un('click', callback);


        b) an other way is to use the ol.Observable.unByKey method, which I like a lot. When calling on or once, it returns a key that references the event. You can then use that key to unlisten your event:



         var key = map.on('click', function(evt) 
        displayFeatureInfo(evt.pixel);
        );
        ol.Observable.unByKey(key);


        I find b) to be more friendly as you can register a bunch of event listeners and put all the keys inside an array. When you want to unregister them all, loop in the array and call the unByKey method, then empty the array. It generates less code that way than having to unregister all events manually.






        share|improve this answer
























          up vote
          13
          down vote



          accepted







          up vote
          13
          down vote



          accepted






          OL3 emmits its own kind of events you could use instead and, to answer your original question, gives an easy and quick way to unregister them.



          Look at this example: http://openlayers.org/en/v3.13.0/examples/vector-layer.html



          More specifically, at these lines:



           map.on('pointermove', function(evt) 
          if (evt.dragging)
          return;

          var pixel = map.getEventPixel(evt.originalEvent);
          displayFeatureInfo(pixel);
          );

          map.on('click', function(evt)
          displayFeatureInfo(evt.pixel);
          );


          The ol.Map object has a on method you can use to register event listeners on the ol3 map browser events. It's best to use those events instead of standard browser events. See the list of all map browser events here: http://openlayers.org/en/v3.13.0/apidoc/ol.MapBrowserEvent.html



          To unregister, you can:



          a) use the un method, but make sure you to give the same callback method as 2nd argument. In other words:



           var callback = function(evt) 
          displayFeatureInfo(evt.pixel);
          ;
          map.on('click', callback);
          map.un('click', callback);


          b) an other way is to use the ol.Observable.unByKey method, which I like a lot. When calling on or once, it returns a key that references the event. You can then use that key to unlisten your event:



           var key = map.on('click', function(evt) 
          displayFeatureInfo(evt.pixel);
          );
          ol.Observable.unByKey(key);


          I find b) to be more friendly as you can register a bunch of event listeners and put all the keys inside an array. When you want to unregister them all, loop in the array and call the unByKey method, then empty the array. It generates less code that way than having to unregister all events manually.






          share|improve this answer














          OL3 emmits its own kind of events you could use instead and, to answer your original question, gives an easy and quick way to unregister them.



          Look at this example: http://openlayers.org/en/v3.13.0/examples/vector-layer.html



          More specifically, at these lines:



           map.on('pointermove', function(evt) 
          if (evt.dragging)
          return;

          var pixel = map.getEventPixel(evt.originalEvent);
          displayFeatureInfo(pixel);
          );

          map.on('click', function(evt)
          displayFeatureInfo(evt.pixel);
          );


          The ol.Map object has a on method you can use to register event listeners on the ol3 map browser events. It's best to use those events instead of standard browser events. See the list of all map browser events here: http://openlayers.org/en/v3.13.0/apidoc/ol.MapBrowserEvent.html



          To unregister, you can:



          a) use the un method, but make sure you to give the same callback method as 2nd argument. In other words:



           var callback = function(evt) 
          displayFeatureInfo(evt.pixel);
          ;
          map.on('click', callback);
          map.un('click', callback);


          b) an other way is to use the ol.Observable.unByKey method, which I like a lot. When calling on or once, it returns a key that references the event. You can then use that key to unlisten your event:



           var key = map.on('click', function(evt) 
          displayFeatureInfo(evt.pixel);
          );
          ol.Observable.unByKey(key);


          I find b) to be more friendly as you can register a bunch of event listeners and put all the keys inside an array. When you want to unregister them all, loop in the array and call the unByKey method, then empty the array. It generates less code that way than having to unregister all events manually.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 9 at 12:52

























          answered Jan 26 '16 at 14:06









          Alexandre Dubé

          2,1951925




          2,1951925






















              up vote
              1
              down vote













              to remove first thing to do is to declare an external function attached to the click
              and then remove it with removeEventListener



              like this



              function myFunction(event)
              //some stuff
              console.log("clicked");


              map.getViewport().addEventListener('click',myFunction);


              and then remove it by



              map.getViewport().removeEventListener('click',myFunction);


              that's all



              Here is a snippet






              function myfunction(e) 
              alert("event click");



              document.getElementById("mybtn").addEventListener("click",myfunction);



              document.getElementById("mybtn2").addEventListener("click",function(e)
              document.getElementById("mybtn").removeEventListener("click",myfunction);
              );

              <button id="mybtn">btn</button><br/>
              <button id="mybtn2" >remove btn click</button>








              share|improve this answer


























                up vote
                1
                down vote













                to remove first thing to do is to declare an external function attached to the click
                and then remove it with removeEventListener



                like this



                function myFunction(event)
                //some stuff
                console.log("clicked");


                map.getViewport().addEventListener('click',myFunction);


                and then remove it by



                map.getViewport().removeEventListener('click',myFunction);


                that's all



                Here is a snippet






                function myfunction(e) 
                alert("event click");



                document.getElementById("mybtn").addEventListener("click",myfunction);



                document.getElementById("mybtn2").addEventListener("click",function(e)
                document.getElementById("mybtn").removeEventListener("click",myfunction);
                );

                <button id="mybtn">btn</button><br/>
                <button id="mybtn2" >remove btn click</button>








                share|improve this answer
























                  up vote
                  1
                  down vote










                  up vote
                  1
                  down vote









                  to remove first thing to do is to declare an external function attached to the click
                  and then remove it with removeEventListener



                  like this



                  function myFunction(event)
                  //some stuff
                  console.log("clicked");


                  map.getViewport().addEventListener('click',myFunction);


                  and then remove it by



                  map.getViewport().removeEventListener('click',myFunction);


                  that's all



                  Here is a snippet






                  function myfunction(e) 
                  alert("event click");



                  document.getElementById("mybtn").addEventListener("click",myfunction);



                  document.getElementById("mybtn2").addEventListener("click",function(e)
                  document.getElementById("mybtn").removeEventListener("click",myfunction);
                  );

                  <button id="mybtn">btn</button><br/>
                  <button id="mybtn2" >remove btn click</button>








                  share|improve this answer














                  to remove first thing to do is to declare an external function attached to the click
                  and then remove it with removeEventListener



                  like this



                  function myFunction(event)
                  //some stuff
                  console.log("clicked");


                  map.getViewport().addEventListener('click',myFunction);


                  and then remove it by



                  map.getViewport().removeEventListener('click',myFunction);


                  that's all



                  Here is a snippet






                  function myfunction(e) 
                  alert("event click");



                  document.getElementById("mybtn").addEventListener("click",myfunction);



                  document.getElementById("mybtn2").addEventListener("click",function(e)
                  document.getElementById("mybtn").removeEventListener("click",myfunction);
                  );

                  <button id="mybtn">btn</button><br/>
                  <button id="mybtn2" >remove btn click</button>








                  function myfunction(e) 
                  alert("event click");



                  document.getElementById("mybtn").addEventListener("click",myfunction);



                  document.getElementById("mybtn2").addEventListener("click",function(e)
                  document.getElementById("mybtn").removeEventListener("click",myfunction);
                  );

                  <button id="mybtn">btn</button><br/>
                  <button id="mybtn2" >remove btn click</button>





                  function myfunction(e) 
                  alert("event click");



                  document.getElementById("mybtn").addEventListener("click",myfunction);



                  document.getElementById("mybtn2").addEventListener("click",function(e)
                  document.getElementById("mybtn").removeEventListener("click",myfunction);
                  );

                  <button id="mybtn">btn</button><br/>
                  <button id="mybtn2" >remove btn click</button>






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Jan 26 '16 at 14:07

























                  answered Jan 26 '16 at 13:58









                  Boo Berr'ita

                  7,23121936




                  7,23121936



























                       

                      draft saved


                      draft discarded















































                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f35015174%2fhow-to-delete-a-listener-in-openlayers-3%23new-answer', 'question_page');

                      );

                      Post as a guest














































































                      Popular posts from this blog

                      How to how show current date and time by default on contact form 7 in WordPress without taking input from user in datetimepicker

                      Syphilis

                      Darth Vader #20