JSFiddle - React, Tailwind, and code Playground
HTML
<script src="https://maps.google.com/maps/api/js?sensor=false&.js"></script>
<button onclick="drawRec();">Draw Rectangle</button>
<div id="map-canvas"></div>
CSS
html, body, #map-canvas {
height: 100%;
margin: 0px;
padding: 10px
}
button {
margin: 0 0 10px;
}
}
JavaScript
var map;
var rectangle;
function initialize() {
var mapOptions = {
center: new google.maps.LatLng(44.5452, -78.5389),
zoom: 9
};
var map = new google.maps.Map(document.getElementById('map-canvas'),
mapOptions);
}
function drawRec() {
var bounds = new google.maps.LatLngBounds(
new google.maps.LatLng(44.490, -78.649),
new google.maps.LatLng(44.599, -78.443));
var rectangle = new google.maps.Rectangle({
bounds: bounds,
editable: true,
draggable: true
});
rectangle.setMap(map);
google.maps.event.addListener(map, 'click', function() {
addRecPath();
});
}
function addRecPath(event) {
var recBounds = rectangle.getBounds();
recBounds.push(event.latLng);
}
google.maps.event.addDomListener(window, 'load', initialize);