Css 将Google Maps容器DIV宽度和高度设置为100%

Css 将Google Maps容器DIV宽度和高度设置为100%,css,google-maps,google-maps-api-3,css-position,google-maps-api-2,Css,Google Maps,Google Maps Api 3,Css Position,Google Maps Api 2,我加载了谷歌地图API v3,并在div中打印谷歌地图。但是当设置宽度和高度为100%和自动时,我看不到地图 下面是HTML代码片段 <!-- Maps Container --> <div id="map_canvas" style="height:100%;width:100px;margin:0 auto;"></div> 是否有办法解决此问题?如果页面上只有div项,请设置: body, html { height: 100%; widt

我加载了谷歌地图API v3,并在
div
中打印谷歌地图。但是当设置宽度和高度为100%和自动时,我看不到地图

下面是HTML代码片段

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


是否有办法解决此问题?

如果页面上只有
div
项,请设置:

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

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

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

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

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

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

希望有帮助。

将贴图容器设置为相对位置以完成此操作。这里是HTML

<body>
    <!-- Map container -->
    <div id="map_canvas"></div>
</body>
<style>
html, body, #map_canvas {
    width: 100%;
    height: 100%;
    margin: 0;
    padding: 0;
}
#map_canvas {
    position: relative;
}
</style>

和简单的CSS

<body>
    <!-- Map container -->
    <div id="map_canvas"></div>
</body>
<style>
html, body, #map_canvas {
    width: 100%;
    height: 100%;
    margin: 0;
    padding: 0;
}
#map_canvas {
    position: relative;
}
</style>

html,正文,#地图#画布{
宽度:100%;
身高:100%;
保证金:0;
填充:0;
}
#地图画布{
位置:相对位置;
}
在所有浏览器上测试。这是屏幕截图

<body>
    <!-- Map container -->
    <div id="map_canvas"></div>
</body>
<style>
html, body, #map_canvas {
    width: 100%;
    height: 100%;
    margin: 0;
    padding: 0;
}
#map_canvas {
    position: relative;
}
</style>

我费了很大劲才找到答案

你真的不需要对体型做任何事情。从映射代码中删除内联样式所需的全部操作:

<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,然后按照您喜欢的方式设置样式。

我刚刚添加了内联样式。


这对我很有效。

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

\map\u canvas\u container{position:relative;}

#map_canvas{位置:绝对;顶部:0;右侧:0;底部:0;左侧:0;}

如果在#map_canvas_容器中有任何非绝对定位的元素,它们将设置其高度,并且贴图将占用确切的可用空间。

这对我来说很有效


map_canvas{位置:绝对;顶部:0;右侧:0;底部:0;左侧:0;}您可以将高度设置为-webkit fill available

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

这是我的工作

    <!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>

无标题文件
#续{
位置:相对位置;
宽度:300px;
高度:300px;
}
#地图画布{
溢出:隐藏;
位置:绝对位置;
排名:0;
右:0;
底部:0;
左:0;
}
函数初始化(){
日志(“初始化…”);
var latlng=新的google.maps.latlng(LAT,LNG);
变量myOptions={
缩放:10,
中心:拉特林,
mapTypeId:google.maps.mapTypeId.ROADMAP
};
var map=new google.maps.map(document.getElementById(“map_canvas”),
肌肽);
}

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

迟做总比不做强!我把我的课改成了一门课:

.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>


div#gmap#u画布包含的元素的样式是什么?`#内容{背景色:#fff;边框:实心1px#a6a196;边框半径:4px;-moz边框半径:4px;高度:自动;溢出:隐藏;}`如果我只希望地图显示在网站的一部分,而不是100%@Machiel@RainbowHat只需在CSS中指定大小,更改div#content的宽度和高度。要清楚,content div中的每个父元素(在OP的代码中是#map canvas,而不是#content)都必须设置特定的宽度/高度,无论是相对的还是精确的。这样做,这个答案是正确的。非常有趣的技术。我做网络开发人员有一段时间了,但我并不知道这一点。很遗憾,否决这个问题的人从来没有说过“为什么”。。。对meGreat技术非常有用。以前从未使用过,而且效果很好。哇,有些东西确实对这个问题有效。难以置信,谢谢!如果您使用的是网格布局,该怎么办?