26

How do I set zoom out limit on the map, it currently lets me zoom out too far to the point that I see multiple world maps: zoom out limit too far

JS:

var map;

var all_coor = <?php echo json_encode($addresses); ?>;
var dub = <?php echo json_encode($testadd); ?>;

function initialize() {
var MainLatlng = new google.maps.LatLng(-74.337724,-49.69693);

  var mapOptions = {
    zoom: 7,
    center: new google.maps.LatLng(31.386692,-12.700747)
  };
  map = new google.maps.Map(document.getElementById('map-canvas'),
      mapOptions);

 var markerr = new google.maps.Marker({
      position: MainLatlng ,
      map: map,
      title: 'Main',
      animation: google.maps.Animation.DROP
  });

for (var t = 0, llen = dub.length; t < llen; t++) {

    var marker = new google.maps.Marker({
      position: new google.maps.LatLng(dub[t]['lat'], dub[t]['long']),
      map: map,
      title: dub[t]['title']
    });

}

}


google.maps.event.addDomListener(window, 'load', initialize);

HTML:

<div id="map-container" style="height: 500px;">
<div id="map-canvas" style="width: 100%; height: 100%"></div>
</div>

1.How can I set a zoom out limit for the map to render properly?

Angad Dubey
  • 5,067
  • 7
  • 30
  • 51

1 Answers1

64

It's been added to the api, you can just set that option directly on the map object:

  map.setOptions({ minZoom: 5, maxZoom: 15 });

ref - https://developers.google.com/maps/documentation/javascript/reference#MapOptions

chrismarx
  • 11,488
  • 9
  • 84
  • 97
  • Additional question: How can I prevent the map from breaking the way it does without having to set custom max zoom out values, i.e. how to make the map behave like a normal map where it stops zooming out once the full world map is visible – Angad Dubey Feb 20 '14 at 16:35
  • I'm not sure I understand your additional question, can you rephrase or elaborate – chrismarx Feb 20 '14 at 18:04
  • The reason I wanted to set zoom out limit was to stop the map from breaking the way it does (as seen in the image above), what would be the right value for the minZoom to show the entire world map.. right now it cuts off australia etc. – Angad Dubey Feb 20 '14 at 18:42
  • ah, well that would depend on size of the map, which might vary if you have flexible sizing, as well as different screen resolutions. I would get the coordinates you want to ensure are shown, then create a glatlngbounds object, and use fitBounds() - see here https://groups.google.com/forum/#!topic/google-maps-js-api-v3/P1M3UOhvw4I – chrismarx Feb 20 '14 at 20:11
  • @maximl337: The thing you want is the `aspect ratio`, which in case of google maps is `1`. At zoom zero the `width/height` of the map for the whole world is `256px` by `256px`. At each zoom level (excluding zoom zero) the formula for width and height is `256 * zoom`. – machineaddict Sep 24 '14 at 12:03
  • 1
    This works in the javascript framework gMap.js too. `var map = new GMaps({minZoom: 5, maxZoom: 15 })`. Thanks. – Leonidas Oct 30 '15 at 13:30