传单:不要在双击时触发点击事件功能

2022-01-12 00:00:00 leaflet javascript

我有一个关于点击传单中地图的问题.如果我点击地图我想在那里设置一个标记,但如果我双击地图我只想放大而不设置标记.所以我有以下代码:

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 setting a marker. So I have the follwing code:

var map = L.map(attrs.id, {
            center: [scope.lat, scope.lng],
            zoom: 14
        });
var marker = L.marker([scope.lat, scope.lng],{draggable: true});
map.on('click', function(event){
            marker.setLatLng(event.latlng);
            marker.addTo(map);                
        });

现在的问题是,当我在地图上双击时,点击事件也会被触发,我想删除该行为.我怎样才能做到这一点?

The problem now is, when I doublclick on the map the click event is also fired and I would like to remove that behavior. How can I achieve that?

谢谢玛格达

推荐答案

所以,我找到了一种方法来做到这一点,我仍然不确定是否有更好的方法来做到这一点.

So, I found a way to do that, I am still not sure, if there is a better way to do it.

var map = L.map(attrs.id, {
        center: [scope.lat, scope.lng],
        zoom: 14
    });
map.clicked = 0;                                                                      
L.tileLayer('http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
        maxZoom: 18
    }).addTo(map);
var marker = L.marker([scope.lat, scope.lng],{draggable: true});
map.on('click', function(event){
    map.clicked = map.clicked + 1;
    setTimeout(function(){
        if(map.clicked == 1){
            marker.setLatLng(event.latlng);
            marker.addTo(map);                
            map.clicked = 0;
        }
     }, 300);
});
map.on('dblclick', function(event){
    map.clicked = 0;
    map.zoomIn();
});

相关文章