How can I listen for the start of a user drawing a polygon in Google Maps v3?

这一生的挚爱 提交于 2019-12-07 14:52:46

问题


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...

  1. Listen to click events on the map.
  2. onclick, check to see which drawing tool is selected (if that's possible).
  3. 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


回答1:


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.




回答2:


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!