I love the new Drawing library for Google Maps v3.7, but I have the feeling the documentation is still not complete and I have a couple of questions:
Documentation only mentions the overlaycompleted
and {overlay}completed
events, but I've also found the drawingmode_changed
event. Is there a list of events somewhere?
Is there a way to getting a reference (object) to the polygon which we are drawing besides waiting until we finish drawing it and using a listener with overlaycompleted
as is done in the above example?
Is there a way can we delete verteces from a polygon, by simply clicking on them? If not, how would you go about it?
You can call any overlay event on complete like this.
google.maps.event.addListener(drawingManager, 'polygoncomplete', function(e) {
//Your magic goes here
});
overlaycomplete
is like a global event for any overlay, such as markers, polylines, circles and polygons. but if you want to create a listener for an specific overlay, just change overlay
to your desired overlay: polygoncomplete
, polylinecomplete
, circlecomplete
.
I know this questions is old, but I wanted to share my answer anyway.
Very old question, but I have some update on this, maybe will help somebody.
There are some additional events for editable shapes that could be useful: https://developers.google.com/maps/documentation/javascript/overlays#user_editable_shapes_events
Also couldn't find solution for this.
This piece of code shows how to remove polygon/polyline vertex on left click:
google.maps.event.addListener(drawingManager, 'overlaycomplete', function (e) {
if (e.type !== google.maps.drawing.OverlayType.MARKER) {
// Switch back to non-drawing mode after drawing a shape.
drawingManager.setDrawingMode(null);
// Add an click event listener for newly-drawn shape
// and remove polygon vertex if it was clicked
var newShape = e.overlay;
google.maps.event.addListener(newShape, 'click', function (e) {
if (e.vertex !== undefined) {
if (newShape.type === google.maps.drawing.OverlayType.POLYGON) {
var path = newShape.getPaths().getAt(e.path);
path.removeAt(e.vertex);
if (path.length < 3) {
newShape.setMap(null);
}
}
if (newShape.type === google.maps.drawing.OverlayType.POLYLINE) {
var path = newShape.getPath();
path.removeAt(e.vertex);
if (path.length < 2) {
newShape.setMap(null);
}
}
}
});
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With