How can I listen to the beginning of a user drawing a polygon in Google Maps v3?

A polygonal event happens here, but I'm looking for a polygonstart event. Even an overlaystart event will work for me.

When the user starts to draw a polygon, I want to delete any existing polygon on the map. I currently have this functionality implemented using the polygoncomplete event. This should happen at the beginning, though.

My pseudo code thought that ...

  • Listen to clicks on events on the map.
  • onclick, check which drawing tool is selected (if possible).
  • If a polygon tool is selected, delete all previous polygons.

That would be a lot easier with the polygonstart event.

Here is a similar question, but hiding and showing drawing controls is not an option for this ui. Limit of the Google Maps graphical manager to 1 polygon

+3
source share
2 answers

This will not work, because according to the docs: note that google.maps.Map events, such as click and mousemove, are disabled when drawing on the map.

You could also test the configuration of map event listeners for click , dblclick .... After activating the drawing manager, there is no response from these listeners if you click on the map. The drawing manager intercepts them. An exception is the map rightclick event, which is still on.

0
source

I was struggling with the issue of listening for clicks on a Google Map canvas today, and I might have discovered an ugly hack / workaround using jQuery:

 setTimeout( function() { var gmDomHackSelect = $('.gm-style').children().eq(0); gmDomHackSelect.click(handleMapCanvasClick); }, 3000); 

This piece of code was from my map initialization function, so I needed a timer to select an item a bit.

The advantage of this listener is that it listens for a click directly on the canvas, i.e. clicks on map controls (e.g. scaling) do not start it.

0
source

Source: https://habr.com/ru/post/1239968/


All Articles