JSFiddle - React, Tailwind, and code Playground
HTML
<script src="https://maps.googleapis.com/maps/api/js?sensor=false&libraries=geometry&ext=.js"></script>
<div id="info"></div>
<div id="mapview"></div>
CSS
html, body, #mapview {
height: 100%;
width: 100%;
}
JavaScript
var map;
var info_window = new google.maps.InfoWindow();
var bounds = new google.maps.LatLngBounds();
var recBounds = [new google.maps.LatLngBounds(new google.maps.LatLng(40.39291, -74.528996), new google.maps.LatLng(41.017537, -73.430364)),
new google.maps.LatLngBounds(new google.maps.LatLng(40.653271, -74.32148846829), new google.maps.LatLng(40.809369, -74.0)),
new google.maps.LatLngBounds(new google.maps.LatLng(42.16092, -71.331815), new google.maps.LatLng(42.465574, -70.782499)),
new google.maps.LatLngBounds(new google.maps.LatLng(39.447088, -105.404428), new google.maps.LatLng(40.08046, -104.305795))];
function initialize() {
var mapOptions = {
zoom: 11,
center: new google.maps.LatLng(51.561918, -0.31237799999996696),
mapTypeId: google.maps.MapTypeId.ROADMAP,
scrollwheel: false
};
map = new google.maps.Map(document.getElementById('mapview'), mapOptions);
var myRect = new google.maps.Rectangle({
bounds: recBounds[0],
map: map
});
var myRectPolygon = new google.maps.Polygon({
map: map,
path: [myRect.getBounds().getNorthEast(), // ne
new google.maps.LatLng(myRect.getBounds().getSouthWest().lat(), myRect.getBounds().getNorthEast().lng()), // se
myRect.getBounds().getSouthWest(),
new google.maps.LatLng(myRect.getBounds().getNorthEast().lat(), myRect.getBounds().getSouthWest().lng()),
myRect.getBounds().getNorthEast()]
});
document.getElementById('info').innerHTML = myRectPolygon.getPath().getLength() + "<br>";
for (var i = 0; i < myRectPolygon.getPath().getLength(); i++) {
document.getElementById('info').innerHTML += myRectPolygon.getPath().getAt(i).toUrlValue(6) + "<br>";
}
/*
var marker1 = new google.maps.Marker({
map: map,
position: myRectPolygon.getPath().getAt(0),
title: "1"
});
var marker2 = new google.maps.Marker({
map: map,
position:...