简体   繁体   English

在传单中双击 CircleMarker 禁用地图缩放

[英]Disable map zoom on CircleMarker double click in leaflet

I'm trying to disable the zoom on the map when I click in a CircleMarker object, but until now, no success.当我单击 CircleMarker 对象时,我试图禁用地图上的缩放,但直到现在,还没有成功。

This is my code:这是我的代码:

var myCircle = new L.CircleMarker(new L.LatLng(50.924480, 10.758276), 10).addTo(map);
myCircle.on("click", function () {
    //my click stuff
});
myCircle.on("dblclick", function () {
    //my dblclick stuff
});

Everytime the dblclick event is fired, the map is zoomed, how to disable it?每次触发 dblclick 事件时,地图都会放大,如何禁用它?

try尝试

var myCircle = new L.CircleMarker(new L.LatLng(50.924480, 10.758276), 10).addTo(map);
map.doubleClickZoom.disable(); 

refer this document参考这个文件

Following solution seems to work for me:以下解决方案似乎对我有用:

myCircle.ondblclick = function (event) {
    event.stopPropagation();
    return false;
};

I have also tried another one, which also works quite well in practice, but I find it a bit hacky:我还尝试了另一个,它在实践中也很有效,但我觉得它有点hacky:

myCircle.on("click", function () {
  map.doubleClickZoom.disable();
  setTimeout(function(){map.doubleClickZoom.enable();}, 1000);
});

If anyone is here looking for a solution to the use case "I want to zoom in on the map on double-click but not if the double-click happens on an entity", this is how I solved it:如果有人在这里寻找用例的解决方案“我想在双击时放大地图,但如果双击发生在实体上则不放大”,我就是这样解决的:

const circle = new L.circlemarker(...).addTo(map);
circle.on("dblclick", () => {
  map.doubleClickZoom.disable();

  doSomething();

  setTimeout(() => {
    map.doubleClickZoom.enable();
  }, 1); // Without the timeout the map will still zoom in on entity double-click
});

FYI event.preventDefault();仅供参考event.preventDefault(); event.stopPropagation(); and return false;return false; inside the "dblclick" handler did not work for me. “dblclick”处理程序内部对我不起作用。

All answers here are after the Leaflet map object has been initialized.这里的所有答案都是在 Leaflet 地图对象初始化之后。

I'd like to give my take by stating that you can also disable doubleClickZoom during map object initialization.我想通过声明您也可以在地图对象初始化期间禁用 doubleClickZoom 来说明我的看法。

Note: This will disable doubleClickZoom to the whole map and not just circleMarkers.注意:这将禁用整个地图的 doubleClickZoom,而不仅仅是 circleMarkers。

map = L.map('map', {
      center: [39.8282, -98.5795],
      zoom: 5,
      doubleClickZoom: false
    });

PS I'm running leaflet version 1.7.1 PS我正在运行传单版本1.7.1

You can return false from your dblclick handler which will stop the event from propagating eg您可以从 dblclick 处理程序return false ,这将阻止事件传播,例如

myCircle.on("dblclick", function () {
    //my dblclick stuff
    return false;
});

Now other elements (such as the map) won't handle that event现在其他元素(例如地图)不会处理该事件

Try this... Disable map.doubleClickZoom when you mouse over the circle and enable when you leave试试这个...当您将鼠标悬停在圆圈上时禁用 map.doubleClickZoom 并在您离开时启用

var myCircle = new L.CircleMarker(new L.LatLng(50.924480, 10.758276), 10).addTo(map);

myCircle
    .on("click", function () {
        //my click stuff
    })
    .on("dblclick", function () {
        //my dblclick stuff
    })
    .on('mouseover', function () {
        map.doubleClickZoom.disable();
    })
    .on('mouseout', function () {
        map.doubleClickZoom.enable();
    });

None of the answers above worked for me:上面的答案都不适合我:

  • calling native ev.originalEvent.preventDefault() did nothing调用本机ev.originalEvent.preventDefault()什么也没做
  • returning false neither都不返回false
  • I don't like the doubleClickZoom workaround that much (although… it works!)我不太喜欢doubleClickZoom解决方法(尽管……它有效!)

Using Leaflet DomEvent did the job for me:使用Leaflet DomEvent为我完成了这项工作:


import { DomEvent } from 'leaflet';

myFeature.on("dblclick", function (ev) {
  DomEvent.stopPropagation(ev)
});

First you need to disable the map double click zoom and then enable it again on the map click event.首先,您需要禁用地图双击缩放,然后在地图单击事件上再次启用它。 So when you double click the map after double clicking on the marker it zoom again ;) I tried it and it works for me perfectly!因此,当您在双击标记后双击地图时,它会再次放大 ;) 我试过了,它对我来说完美无缺! Enjoy!享受!

map.doubleClickZoom.disable();

map.on('click', function (e) { 
  map.doubleClickZoom.enable();
 });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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