leaflet: don't fire click event function on doubleclick

后端 未结 3 1734
庸人自扰
庸人自扰 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:33

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

提交回复
热议问题