In my application using Leaflet.draw the user can set point markers on a map.

I would like to allow the setting only in a certain area.

The function isMarkerInsidePolygon(marker, polygon) returns true, if the marker is inside the polygone, false if outside.

For this purpose, I think I have to check after draw:drawstart, but before draw:created, if isMarkerInsidePolygon(marker, polygon) returns true or false.

If it returns false, I want to prevent setting the marker, but still keep the drawing mode active. Is there a posibility to prevent the setting of a marker in leaflet.draw?

There is a similar question asked by Ben, but no exactly the same case:
Leaflet draw: prevent drawing through a custom overlay

1 Answer 1


Since there seems to be no official hook to implement this, one has to resort to hack.

L.Draw.Marker class has internal _onClick method which is called when user clicks with mouse to create marker. This method can be replace with our own hacked method by modifying L.Draw.Marker class. We can check here if clicked point is within polygon and allow creation of marker or return immediately without creating it.

  _onClick: function () {
    // our hack
    if (! isMarkerInsidePolygon(this._marker, polygon)) return;
    // original code
    if (this.options.repeatMode) {
  • Thank you for your suggestion. That sounds plausible for me. I will give it a try and let you know if it works next week.
    – pascatl
    May 3, 2019 at 20:39
  • Since no new class is created, just existing one modified, I replaced extend method with more appropriate include method (see leafletjs.com/examples/extending/extending-1-classes.html).
    – TomazicM
    May 4, 2019 at 10:38
  • @pascatl So did you try it?
    – TomazicM
    May 15, 2019 at 11:13
  • I gave it a short try, but actually I don't know how to make my polygon accessible for the function isMarkerInsidePolygon(this._marker, polygon) inside the L.Draw.Marker class :/
    – pascatl
    May 15, 2019 at 11:36
  • It's hard to give advice without seeing/knowing logic of your code. First answer woud be to define function at top level so it's accessible everywhere.
    – TomazicM
    May 15, 2019 at 16:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.