将Google Maps Container DIV的宽度和高度设置为100%

时间:2012-04-18 12:49:11

标签: css google-maps google-maps-api-3 css-position google-maps-api-2

我加载了Google Maps API v3并在div中打印了Google地图。但是当设定宽度&高度为100%和自动我无法看到地图。

这是HTML代码段。

<!-- Maps Container -->
<div id="map_canvas" style="height:100%;width:100px;margin:0 auto;"></div>

有没有办法解决这个问题?

12 个答案:

答案 0 :(得分:65)

如果要用它覆盖整个页面,则必须将所有父容器设置为100%宽度。您必须至少为#content div设置宽度和高度的绝对值。

body, html {
  height: 100%;
  width: 100%;
}

div#content {
  width: 100%; height: 100%;
}

答案 1 :(得分:41)

将地图容器设置为相对的位置。这是 HTML

<body>
    <!-- Map container -->
    <div id="map_canvas"></div>
</body>

简单 CSS

<style>
html, body, #map_canvas {
    width: 100%;
    height: 100%;
    margin: 0;
    padding: 0;
}
#map_canvas {
    position: relative;
}
</style>

在所有浏览器上测试过。这是屏幕截图

enter image description here

答案 2 :(得分:37)

很少有人意识到css定位的力量。要将地图设置为占据其父容器的100%高度,请执行以下操作:

  

#map_canvas_container {position: relative;}

     

#map_canvas {position: absolute; top: 0; right: 0; bottom: 0; left: 0;}

如果你在#map_canvas_container中有任何非绝对定位的元素,他们将设置它的高度,地图将占用确切的可用空间。

答案 3 :(得分:3)

这对我而言。

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<style type="text/css"> 

#cont{
    position: relative;
    width: 300px;
    height: 300px;
}
#map_canvas{
    overflow: hidden;
    position: absolute;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
}
</style> 
<script type="text/javascript" src="http://maps.google.com/maps/api/js?key=APIKEY"></script>
<script type="text/javascript">
function initialize() {
    console.log("Initializing...");
  var latlng = new google.maps.LatLng(LAT, LNG);
    var myOptions = {
      zoom: 10,
      center: latlng,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    };
    var map = new google.maps.Map(document.getElementById("map_canvas"),
        myOptions);
}
</script>
</head>

<body onload="initialize()">
<div id="cont">
<div id="map_canvas" style="width: 100%; height: 100%;"></div>
</div>
</body>
</html>

答案 4 :(得分:0)

Gmap将内联样式位置写入相对于div的位置。用以下内容覆盖:

google.maps.event.addListener(map, 'tilesloaded', function(){
    document.getElementById('maps').style.position = 'static';
    document.getElementById('maps').style.background = 'none';
});

希望它有所帮助。

答案 5 :(得分:0)

您可以将高度设置为-webkit-fill-available

<!-- Maps Container -->
<div id="map_canvas" style="height:-webkit-fill-available;width:100px;"></div>

答案 6 :(得分:0)

如果您不能影响父元素(例如在嵌套组件的情况下),则可以使用height: 100vh使其成为全窗口(=视图)高度;

答案 7 :(得分:-1)

迟到总比不到好!我让我上了一堂课:

.map
{
    position:absolute;
    top:64px;
    width:1100px;
    height:735px;
    overflow:hidden;
    border:1px solid rgb(211,211,211);
    border-radius:3px;
}

然后

<div id="map" class="map"></div>

答案 8 :(得分:-3)

如果div是您网页上的唯一内容,请设置:

body, html {
  height: 100%;
  width: 100%;
}

答案 9 :(得分:-3)

我努力寻找答案。

你真的不需要做任何身体尺寸的事情。您只需从地图代码中删除内联样式:

<iframe width="425" height="350" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="https://maps.google.co.uk/maps?f=q&amp;source=s_q&amp;hl=en&amp;geocode=&amp;q=new+york&amp;aq=&amp;sll=53.546224,-2.106543&amp;sspn=0.02453,0.084543&amp;ie=UTF8&amp;hq=&amp;hnear=New+York,+United+States&amp;t=m&amp;z=10&amp;iwloc=A&amp;output=embed"></iframe><br /><small><a href="https://maps.google.co.uk/maps?f=q&amp;source=embed&amp;hl=en&amp;geocode=&amp;q=new+york&amp;aq=&amp;sll=53.546224,-2.106543&amp;sspn=0.02453,0.084543&amp;ie=UTF8&amp;hq=&amp;hnear=New+York,+United+States&amp;t=m&amp;z=10&amp;iwloc=A" style="color:#0000FF;text-align:left">View Larger Map</a></small>

删除所有内联样式并添加类或ID,然后按照您喜欢的方式设置样式。

答案 10 :(得分:-5)

这对我有用。

map_canvas {position:absolute;顶部:0;右:0;底部:0; left:0;}

答案 11 :(得分:-6)

我刚添加了内联样式。
<div id="map_canvas" style="width:750px;height:484px;"></div>
它对我有用。

相关问题