javascript 在传单中双击 CircleMarker 禁用地图缩放
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15406537/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Disable map zoom on CircleMarker double click in leaflet
提问by vaibhav shah
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 事件时,地图都会缩放,如何禁用它?
回答by
try
尝试
var myCircle = new L.CircleMarker(new L.LatLng(50.924480, 10.758276), 10).addTo(map);
map.doubleClickZoom.disable();
referthis document
参考这个文件
回答by Suma
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);
});
回答by vobango
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.stopPropagation();
and return false;
inside the "dblclick" handler did not work for me.
仅供参考event.preventDefault();
event.stopPropagation();
,return false;
在“dblclick”处理程序中对我不起作用。
回答by luisbello30
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();
});
回答by anonymous anonymous
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();
});
回答by lee penkman
You can return false
from your dblclick handler which will stop the event from propagating e.g.
您可以return false
从您的 dblclick 处理程序中停止事件的传播,例如
myCircle.on("dblclick", function () {
//my dblclick stuff
return false;
});
Now other elements (such as the map) won't handle that event
现在其他元素(例如地图)不会处理该事件