leaflet: don't fire click event function on doubleclick

后端 未结 3 1763
庸人自扰
庸人自扰 2021-01-03 08:51

I have a question concerning clicks on a map in leaflet. If I click on the map I want to set a marker there, but if doubleclick on the map I just want to zoom in without set

3条回答
  •  星月不相逢
    2021-01-03 09:16

    This is still an issue on recent (leaflet 1.4) versions.

    Alternative approach I used that:

    • legit usage of setTimeout and clearTimeout
    • without adding random props to the map object
    • no jQuery:
    map.on('click', function(event) {
      if (_dblClickTimer !== null) {
        return;
      }
      _dblClickTimer = setTimeout(() => {
    
        // real 'click' event handler here
    
        _dblClickTimer = null;
      }, 200);
    })
    .on("dblclick", function() {
      clearTimeout(_dblClickTimer);
      _dblClickTimer = null;
    
      // real 'dblclick' handler here (if any). Do not add anything to just have the default zoom behavior
    });
    

    Note that the 200 ms delay must be tested. On my environment using a value like 100 was not enough as the doubleclick event is triggered with a delay.

提交回复
热议问题