谷歌地图 - 填充剩余高度

时间:2017-12-15 08:48:33

标签: javascript html css google-maps google-maps-api-3

我正在制作地图应用程序,我希望谷歌地图DIV填充标题下方的剩余高度。代码如下。

  <!DOCTYPE html>
  <head>
    <title>Map Application</title>

    <style type="text/css">
    html {
        height: 100%;
    }
    body {
        height: 100%;
        margin: 0;
        padding: 0;
    }
    #map_canvas {
        margin: 0;
        padding: 0;
        height: 95%;
        width: 100%;
    }
    @media (max-width: 767px) {
        #start_lat, #start_lng, #dest_lat{
          margin-bottom: 0.5em;
      }
  }
</style>

<!-- Bootstrap -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/css/bootstrap.min.css" integrity="sha384-PsH8R72JQ3SOdhVi3uxftmaW6Vc51MKb0q5P2rRUpPvrszuE4W1povHYgTpBfshb" crossorigin="anonymous">

<!-- Fontawesome -->
<script src="https://use.fontawesome.com/2eede759bf.js"></script>

<script type="text/javascript">
  var map;
  var markers = [];
  var path;
  var bounds;
  function initialize() {
    var mapOptions = {
      center: new google.maps.LatLng(-25.363, 131.044),
      zoom: 15,
      mapTypeId: google.maps.MapTypeId.ROADMAP
  };
  map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);
}
</script>
</head>
<body>
    <nav class="navbar navbar-expand-lg navbar-light bg-light">
      <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
          <span class="navbar-toggler-icon"></span>
      </button>
      <div class="collapse navbar-collapse" id="navbarSupportedContent">
        <form class="form-inline my-2 my-lg-0 mx-auto" id="map-data" action="" method="POST" align="center">
          <!-- Here goes a form with a few input fields and buttons. -->
        </form>
    </div>
</nav>
<div id="map_canvas"></div>
<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?key={{ GMAPS_API_KEY }}&callback=initialize"></script>
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.3/umd/popper.min.js" integrity="sha384-vFJXuSJphROIrBnz7yo7oB41mKfc8JzQZiCq4NCceLEaO4IHwicKwpJf9c9IpFgh" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/js/bootstrap.min.js" integrity="sha384-alpBpkh1PFOepccYVYDB4do5UnbKysX5WZXm3XxPqe5iKTfUKjNkCk9SaVuEZflJ" crossorigin="anonymous"></script>
</body>
</html>

我尝试过绝对定位,但似乎Google Map覆盖了相对定位,请提供建议。

谢谢。

2 个答案:

答案 0 :(得分:3)

你可以给<nav>一个固定的高度,并使用calc()来计算#map_canvas的高度:

.navbar {
    height:150px;
}

#map_canvas {
    height:calc(100vh - 150px);
}

否则你可以使用flexbox:

body {
    display:flex;
    flex-direction:column;
    min-height:100vh;
}

.navbar {
    flex-grow:0;
}

#map_canvas {
    flex-grow:1;
}

答案 1 :(得分:-1)

试试这个:

#map_canvas {
position: absolute;
top: 0px;
}

尝试为#map_canvas提供绝对位置和顶部:0px。这应该做。