admin管理员组文章数量:1334917
So, I have a leaflet map...
var map = L.map('map').setView([35.772219, -78.675272], 17);
map.on('click', function(e) {alert('map click!')});
and I add a marker...
var marker = L.circleMarker([35.772219, -78.675272]);
marker.on('click', function(e) {alert('marker click!')});
marker.addTo(map);
If I click on the marker, both the marker and map click events fire, but I only want the click event for the marker... Is there any way to acplish this? I can't seem to find one in the docs.
So, I have a leaflet map...
var map = L.map('map').setView([35.772219, -78.675272], 17);
map.on('click', function(e) {alert('map click!')});
and I add a marker...
var marker = L.circleMarker([35.772219, -78.675272]);
marker.on('click', function(e) {alert('marker click!')});
marker.addTo(map);
If I click on the marker, both the marker and map click events fire, but I only want the click event for the marker... Is there any way to acplish this? I can't seem to find one in the docs.
Share Improve this question asked Jan 29, 2020 at 15:50 John ChrysostomJohn Chrysostom 3,9831 gold badge42 silver badges55 bronze badges3 Answers
Reset to default 10L.DomEvent.stopPropagation
should do the trick:
Stop the given event from propagation to parent elements. Used inside the listener functions:
marker.on('click', function(e) {
L.DomEvent.stopPropagation(e);
console.log('marker click!')
});
And a demo (one circle cancels its event and not the other)
var map = L.map('map').setView([35.772219, -78.675272], 10);
map.on('click', function(e) {console.log('map click!')});
var marker = L.circleMarker([35.772219, -78.68]);
marker.on('click', function(e) {
L.DomEvent.stopPropagation(e);
console.log('marker click!')
});
marker.addTo(map);
marker = L.circleMarker([35.772219, -78.63], {fillColor: 'red'});
marker.on('click', function(e) {
console.log('marker click with map click')
});
marker.addTo(map);
<link rel="stylesheet" href="https://unpkg./[email protected]/dist/leaflet.css" />
<script src="https://unpkg./[email protected]/dist/leaflet.js"></script>
<div id="map" style="height: 100px"></div>
Have you try to add a e.preventDefault(); and e.stopPropagation() in the function of your marker click? Like so:
marker.on('click', function(e) { e.preventDefault(); e.stopPropagation(); alert('marker click!');});
use event.stopPropagation()
, to stop event bubbling to the parent. See stopPropagation.
document.querySelector('.parent').addEventListener('click', () => {
console.log('Listening parent');
});
document.querySelector('.child').addEventListener('click', (e) => {
e.stopPropagation();
console.log('Listening child');
});
<div class="parent">
parent
<div class="child">
child
</div>
</div>
本文标签: javascriptDon39t generate map click events when marker clicked in leafletStack Overflow
版权声明:本文标题:javascript - Don't generate map click events when marker clicked in leaflet - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742376972a2463355.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论