There's a polygoncomplete event that is fired, but I'm looking for a polygonstart event. Even an overlaystart event would work for me.
When the user starts to draw a polygon, I want to remove any existing polygon on the map. Currently, I have that functionality implemented using the polygoncomplete event. It needs to happen at the start though.
My pseudocode thought is to...
- Listen to click events on the map.
- onclick, check to see which drawing tool is selected (if that's possible).
- If polygon tool selected, remove all previous polygons.
This would be much easier with a polygonstart event.
Here is a similar question, but hiding and showing the drawing controls is not an option for this ui. Google Maps Drawing Manager limit to 1 polygon
I was wrestling with the problem of listening for clicks on the Google Map canvas today, and I may have discovered an ugly hack/workaround using jQuery:
setTimeout( function() {
var gmDomHackSelect = $('.gm-style').children().eq(0);
gmDomHackSelect.click(handleMapCanvasClick);
}, 3000);
This fragment of code was from my map initialisation function, so I needed a timer to select the element a little while later.
The advantage of this listener is that it listens for click directly on the canvas, ie. clicks on map controls (eg zoom) do not trigger it.
This won't work because according to docs: Note that google.maps.Map events, such as click and mousemove are disabled while drawing on the map.
That could be also tested setting map event listeners for click
, dblclick
... events. After activating drawing manager there is no response from those listeners if you click on the map. Drawing manager is intercepting them. Exception is map rightclick
event which is still enabled.
来源:https://stackoverflow.com/questions/22458226/how-can-i-listen-for-the-start-of-a-user-drawing-a-polygon-in-google-maps-v3