Things you miss
- You need to initialize the Google Maps API API.
- You must initialize the Draw Manager object to draw shapes.
Follow the tutorial for reference.
. / google. Google API- Google . (, , , )
.
JS
var map;
var drawingManager = new google.maps.drawing.DrawingManager();
function initialize() {
var mapOptions = {
center: new google.maps.LatLng(44.5452, -78.5389),
zoom: 9
};
map = new google.maps.Map(document.getElementById('map-canvas'),
mapOptions);
}
function drawRec() {
drawingManager.setOptions({
drawingMode : google.maps.drawing.OverlayType.RECTANGLE,
drawingControl : true,
drawingControlOptions : {
position : google.maps.ControlPosition.TOP_CENTER,
drawingModes : [ google.maps.drawing.OverlayType.RECTANGLE ]
},
rectangleOptions : {
strokeColor : '#6c6c6c',
strokeWeight : 3.5,
fillColor : '#926239',
fillOpacity : 0.6,
editable: true,
draggable: true
}
});
drawingManager.setMap(map);
}
google.maps.event.addDomListener(window, 'load', initialize);
HTML
<button onclick="drawRec();">Draw Rectangle</button>
<div id="map-canvas"></div>
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false&libraries=drawing"></script>