google maps height 100% of div parent

前端 未结 1 599
悲&欢浪女
悲&欢浪女 2020-12-01 22:22

Is it possible make a google map height 100% of the parent div? I noticed that with the height 100%, the map is not visible, but if I add the height of the div with the map

1条回答
  •  我在风中等你
    2020-12-01 22:53

    reference: Mike Williams' Google Maps Javascript API v2 tutorial page: Using a percentage height for the map div

    If you try to use style="width:100%;height:100%" on your map div, you get a map div that has zero height. That's because the div tries to be a percentage of the size of the , but by default the has an indeterminate height. There are ways to determine the height of the screen and use that number of pixels as the height of the map div, but a simple alternative is to change the so that its height is 100% of the page. We can do this by applying style="height:100%" to both the and the . (We have to do it to both, otherwise the tries to be 100% of the height of the document, and the default for that is an indeterminate height.)

    code snippet:

    var map;
    
    function initialize() {
      var map = new google.maps.Map(
        document.getElementById("map"), {
          center: new google.maps.LatLng(37.4419, -122.1419),
          zoom: 13,
          mapTypeId: google.maps.MapTypeId.ROADMAP
        });
    
    }
    google.maps.event.addDomListener(window, "load", initialize);
    html,
    body {
      height: 100%;
      width: 100%;
      margin: 0px;
      padding: 0px
    }
    
    

    0 讨论(0)
提交回复
热议问题