Removing polygon from google maps drawingManager V3
For a single polygon overlay I did this:
var _myPolygon; // global variable to store the polygon instance
google.maps.event.addListener(drawingManager, 'overlaycomplete', function(e) {
_myPolygon = e.overlay;
});
$('#delete-button').on('click', function() {
_myPolygon.setMap(null);
});
You need a reference to the polygon in the global context. Then in the click handler function for the button call polygon.setMap(null) (where polygon is a global reference to the polygon, can't tell if it is global or not from the incomplete snippet you posted)
Examine this code, it sounds exactly like what you are talking about, a button to delete the shape
http://gmaps-samples-v3.googlecode.com/svn-history/r282/trunk/drawing/drawing-tools.html
Edit: above link is broken but I was able to find that code here.
// globals
var drawingManager;
var selectedShape;
...
function clearSelection() {
if (selectedShape) {
selectedShape.setEditable(false);
selectedShape = null;
}
}
function setSelection(shape) {
clearSelection();
selectedShape = shape;
shape.setEditable(true);
selectColor(shape.get('fillColor') || shape.get('strokeColor'));
}
function deleteSelectedShape() {
if (selectedShape) {
selectedShape.setMap(null);
}
}
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 event listener that selects the newly-drawn shape when the user
// mouses down on it.
var newShape = e.overlay;
newShape.type = e.type;
google.maps.event.addListener(newShape, 'click', function() {
setSelection(newShape);
});
setSelection(newShape);
}
});
// Clear the current selection when the drawing mode is changed, or when the
// map is clicked.
google.maps.event.addListener(drawingManager, 'drawingmode_changed', clearSelection);
google.maps.event.addListener(map, 'click', clearSelection);
google.maps.event.addDomListener(document.getElementById('delete-button'), 'click', deleteSelectedShape);
buildColorPalette();
}