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?
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.
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.
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);
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); });
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With