0
votes

Can you please take a look at this Demo and let me know how I can enable Google Maps to zoom in to the map after it is drawn?

var map;
var drawingManager
$(document).ready(function () {
    var latlng = new google.maps.LatLng(49.241943, -122.889318);
    var myOptions = {
        zoom: 12,
        center: latlng,
        disableDefaultUI: true,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    };

    map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);

    drawingManager = new google.maps.drawing.DrawingManager({
        drawingMode: google.maps.drawing.OverlayType.MARKER,
        drawingControl: true,
        drawingControlOptions: {
            position: google.maps.ControlPosition.TOP_CENTER,
            drawingModes: [
                   google.maps.drawing.OverlayType.RECTANGLE]
        }
    });
    drawingManager.setMap(map);

});
1

1 Answers

1
votes

add a 'rectanglecomplete' listener:

google.maps.event.addListener(drawingManager,'rectanglecomplete',function(r) {
    map.fitBounds(r.getBounds());
});

updated fiddle

code snippet:

var map;
var drawingManager
$(document).ready(function() {
  var latlng = new google.maps.LatLng(49.241943, -122.889318);
  var myOptions = {
    zoom: 12,
    center: latlng,
    disableDefaultUI: true,
    mapTypeId: google.maps.MapTypeId.ROADMAP
  };

  map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);

  drawingManager = new google.maps.drawing.DrawingManager({
    drawingMode: google.maps.drawing.OverlayType.MARKER,
    drawingControl: true,
    drawingControlOptions: {
      position: google.maps.ControlPosition.TOP_CENTER,
      drawingModes: [
        google.maps.drawing.OverlayType.RECTANGLE
      ]
    }
  });
  google.maps.event.addListener(drawingManager, 'rectanglecomplete', function(r) {
    map.fitBounds(r.getBounds());
  });
  drawingManager.setMap(map);

});
body {
  padding-top: 25px;
}
#map_canvas {
  width: 100%;
  height: 400px;
}
<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?v=3.exp&sensor=false&libraries=drawing"></script>
<div class="container">
  <div class="well">
    <div id="map_canvas"></div>
  </div>
</div>