1

Using fitBounds function of Google Map API v3 always makes the bound smaller than the canvas size as follwing:

gMap.fitBounds(bounds);
new google.maps.Rectangle({
                    strokeColor: '#0000FF',
                    strokeOpacity: 0.8,
                    strokeWeight: 1,
                    fillColor: '#0000FF',
                    fillOpacity: 0.05,
                    map: gMap,
                    bounds: bounds
                });

enter image description here

Is there a way to make the bound fit with the canvas size?

Jin Ho
  • 3,565
  • 5
  • 23
  • 25

2 Answers2

0

Calculate the map center and zoom required to fit your bounds yourself, then set them using map.setOptions({center:calculatedCenter, zoom: calculatedZoom});

One possible way to do that: Calculate bounds from center and zoom (Google Maps API v3) (calculates the bounds from the center and zoom, you need to calculate the zoom level from the bounds and the div size)

Community
  • 1
  • 1
geocodezip
  • 158,664
  • 13
  • 220
  • 245
0

You cannot always have the bounds completely fill the canvas because zoom levels are discrete. That is, the zoom level can be set only to whole numbers, not fractions.

Using gMap.fitBounds() is one way to zoom the map so the entire bounds is displayed on the canvas, but it sometimes zooms in farther than necessary. If that is not acceptable, you can calculate the zoom level using the function in this answer.

Community
  • 1
  • 1
John S
  • 21,212
  • 8
  • 46
  • 56