I am currently working on a map based web application. The previous rectangle (or polygon) that has already been drawn must be deleted before I draw a new rectangle by drawing a layer tool on the Google map.
There is an overlaycomplete event on Google Map , but if I use it, the previous rectangle will be deleted after the new rectangle is completed (mouse). However, I need to delete the previous rectangle before the process of drawing a new rectangle begins (mouse-down) Two rectangles should not be displayed on the map at the same time!
All reviews are appreciated!
Here is jsFiddle! - http://jsfiddle.net/sean35/41Lrcq7L/30/
Code example:
function initialize() {
var centerInfo = document.getElementById("mainForm:centerInfo").value;
var zoomInfo = document.getElementById("mainForm:zoomInfo").value;
centerInfo = centerInfo.split(",");
var mapOptions = {
center : {
lat : parseFloat(centerInfo[0]),
lng : parseFloat(centerInfo[1])
},
zoom : parseInt(zoomInfo)
};
map = new google.maps.Map(document.getElementById('deviceControlMap'), mapOptions);
var drawingManager = new google.maps.drawing.DrawingManager({
drawingControl : true,
drawingControlOptions : {
position : google.maps.ControlPosition.TOP_CENTER,
drawingModes : [
google.maps.drawing.OverlayType.RECTANGLE ]
},
rectangleOptions : {
strokeWeight : 1,
clickable : true,
editable : false,
zIndex : 1
}
});
drawingManager.setMap(map);
google.maps.event.addListener(drawingManager, 'overlaycomplete', function(event) {
if (event.type == google.maps.drawing.OverlayType.RECTANGLE) {
if(rectangle != null)
rectangle.setMap(null);
closeDimSubWin();
rectangle = event.overlay;
var bounds = rectangle.getBounds();
console.log(bounds);
}
});
google.maps.event.addListener(drawingManager, "drawingmode_changed", function() {
if(rectangle != null)
rectangle.setMap(null);
});
google.maps.event.addListener(map, 'click', function() {
if(rectangle != null)
rectangle.setMap(null);
});
}
google.maps.event.addDomListener(window, 'load', initialize);
EDIT: I wrote two similar questions before. But I deleted them and wrote this question with extended information. I hope the question is clear.
source
share