Reputation: 447
I want there to be a reaction to when a leaflet map is clicked on, and an 'alert' to happen. But the thing is, I only want that reaction to happen if the click was outside of some polygon shapes I have on the map. I've tried:
function onMapClick(e) {
alert("Nothing to do here") ;
}
mymap.on('click', onMapClick);
It works fine if I don't click on one of my polygons. But, if I click on a polygon, I'll get their actions, plus still get this alert, which I don't want to happen. This is a possibility I can't find anybody mention anywhere, so I feel like I've run out of leads. Where should I look to learn how to do this?
Upvotes: 0
Views: 1279
Reputation: 1878
You can try this trick, Insert a click event on Polygon:
var onClick = function(event){
//Do nothing
};
var poly = new L.Polygon([
[79.07181, -100.63477],
[79.06348, -90.43945],
[77.52312, -90.52734],
[77.50412, -94.21875],
[77.41825, -94.35059],
]);
poly.on('click', onClick);
Upvotes: 0
Reputation: 19089
In Leaflet 1.0.x, interactive events on some map layers (such as L.Polygon
s) bubble up to the map by default. See bug #3307 for details and background.
The documentation doesn't mention this explicitly, but hopefully will get better with PR #4883. Just call L.DomEvent.stopPropagation()
as needed, inside the polygon event handler.
Upvotes: 1