Google Maps API V3: Show the whole world

后端 未结 5 1431
小蘑菇
小蘑菇 2020-12-08 11:25

How can I programmatically zoom the map so that it covers the whole world?

5条回答
  •  旧时难觅i
    2020-12-08 11:54

    It's not guaranteed to not have repeats, but by abusing the fact the base tile starts at 256 (zoom 0) and doubles at each increment there on in -- you can select the optimum zoom level using a logarithm function.

    function initialize () {
      var mapOptions = {
        center: new google.maps.LatLng(0, 0),
        zoom: Math.ceil(Math.log2($(window).width())) - 8,
      };
      
      var map = new google.maps.Map(document.getElementById('map'), mapOptions);
    }
    
    google.maps.event.addDomListener(window, 'load', initialize);
    #map {
      width: 100%;
      height: 500px;
    }
    
       
        
        
      
      
        

提交回复
热议问题