How to change the zoom level of Google maps programmatically?

前端 未结 2 2089
死守一世寂寞
死守一世寂寞 2021-01-01 10:37
function displayMapAndClick ()
    {
        var latlng    = new google.maps.LatLng (29.0167, 77.3833);
        var myOptions = 
        {
            zoom:zm,
              


        
相关标签:
2条回答
  • 2021-01-01 11:10

    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);
    });
    
    0 讨论(0)
  • 2021-01-01 11:29

    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);
    
    0 讨论(0)
提交回复
热议问题