It looks like google.maps.Rectangle.getBounds returns "invalid" restrictions
key:Vince posn:(53.801279, -1.5485670000000482) out of bnds:((55.45394132943307, -4.0869140625), (52.72298552457069, 1.7138671875)) key:Vince posn:(53.801279, -1.5485670000000482) in bnds:((52.26815737376817, -4.04296875), (55.65279803318956, 2.2412109375))
If you extend the empty google.maps.LatLng object with your two corners, it will work:
google.maps.event.addListener(themap, 'mousemove', function (e) { if (mouseIsDown && shiftPressed) { if (gribBoundingBox !== null) // box exists { bounds.extend(e.latLng); gribBoundingBox.setBounds(bounds); // If this statement is enabled, I lose mouseUp events } else // create bounding box { bounds = new google.maps.LatLngBounds(); bounds.extend(e.latLng); gribBoundingBox = new google.maps.Rectangle({ map: themap, bounds: bounds, fillOpacity: 0.15, strokeWeight: 0.9, clickable: false }); } } });
working example
code snippet:
var map; var markers = {}; var bounds = null;
#map { height: 500px; width: 500px; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="https://maps.googleapis.com/maps/api/js"></script> <div id="map"></div> <div id="info"></div>
source share