5

I'm using the Google Maps Drawing Manager to allow users to draw a polygon.

The users can draw a polygon, but it has to be a "simple" polygon, not "complex" i.e. it cannot be self-intersecting.

The Google Maps library doesn't seem to have any built-in support for detecting/preventing self-intersection so I planned to listen for the user creating each point after which I'd run a detection algorithm to check if the current polyline/linestring is self intersecting.

Unfortunately the only events in the documentation are the overlaycomplete events.

I could wait until the user has finished drawing their polygon before validating it and telling them it's not allowed, but it would be a much better experience if I could stop them from ever placing the point in the first place, potentially saving them a lot of wasted time and effort.

I could build my own drawing tool with the functionality I need, but the creation and maintenance of such a thing is something I'd rather avoid when Google have an offering that's so very close to what I need.

I already have the validation code ready to go, I'm using the JSTS code provided by similar questions here on SO, it's the Drawing Manager functionality that I'm asking about.

Community
  • 1
  • 1

1 Answers1

3

I found that there's no documented event for when the user places each point in the polygon, you only have the event for when it's finished creating the polygon.

So I just have to use that, until I have time to make my own drawing manager from scratch.

Also the JSTS turned out to be very large (in minified file size) so I dropped that.

Since my polygons will always be low in point count I went for a much simpler approach where I tested every line in the polygon to see if they intersect with any other line in the polygon. It's rather "brute force" but it's still fast enough to be unnoticed by the end user.