Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change the zoom level of Google maps programmatically?

function displayMapAndClick ()     {         var latlng    = new google.maps.LatLng (29.0167, 77.3833);         var myOptions =          {             zoom:zm,             center:latlng,             mapTypeId:google.maps.MapTypeId.ROADMAP         };          map = new google.maps.Map (document.getElementById ("map"), myOptions);         directionsDisplay.setMap (map);      } 

Where zm is a global variable set to default 7.

Now, I wish to change the zoom level of this map thorough this program.

What is the way to do that without re-initializing the map, or is re-initializing the map compulsory?

like image 489
Aquarius_Girl Avatar asked Sep 17 '12 11:09

Aquarius_Girl


People also ask

How do I change the max zoom level in Google Maps?

Setting up the minimum or maximum Zoom value for Google Maps can be done by adding the shortcode attribute with Store Locator Shortcode. Furthermore, the maximum value of Google Maps Zoom level is 22. Therefore, the defined maximum value must be below or equal to 22, which is the default.

How do I zoom in Google Maps API?

Users can zoom the map by clicking the zoom controls. They can also zoom and pan by using two-finger movements on the map for touchscreen devices.


2 Answers

Use the setZoom() method from the google.maps.Map class.

var mapOptions = {   /* Initial zoom level */   zoom: 8   ... }; map = new google.maps.Map(..., mapOptions); /* Change zoom level to 12  */ map.setZoom(12); 
like image 154
Alexander Avatar answered Sep 23 '22 11:09

Alexander


In addition to Alexanders' sollution: I had the same problem, but the above didn't work for me in all browsers because sometimes the map.setZoom() is executed before the map is done loading.

Wrapping the function like this will make it work always:

... map = new google.maps.Map(..., mapOptions); /* Change zoom level to 12  */ google.maps.event.addListenerOnce(map, 'bounds_changed', function() {   map.setZoom(12); }); 
like image 22
publicJorn Avatar answered Sep 23 '22 11:09

publicJorn