我加载了Google Maps API v3并在div
中显示了Google地图。但是当将宽度和高度设置为100%和auto时,无法看到地图。
以下是HTML代码片段。
<!-- Maps Container -->
<div id="map_canvas" style="height:100%;width:100px;margin:0 auto;"></div>
有没有办法解决这个问题?
我加载了Google Maps API v3并在div
中显示了Google地图。但是当将宽度和高度设置为100%和auto时,无法看到地图。
以下是HTML代码片段。
<!-- Maps Container -->
<div id="map_canvas" style="height:100%;width:100px;margin:0 auto;"></div>
有没有办法解决这个问题?
如果您想覆盖整个页面,必须将所有父容器的宽度设置为100%。至少,您需要为#content div设置绝对值的宽度和高度。
body, html {
height: 100%;
width: 100%;
}
div#content {
width: 100%; height: 100%;
}
<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>
已在所有浏览器上进行测试。这里是屏幕截图。
如果在#map_canvas_container内有任何非绝对定位的元素,它们将设置其高度,而地图将占用确切的可用空间。
#map_canvas_container {position: relative;}
#map_canvas {position: absolute; top: 0; right: 0; bottom: 0; left: 0;}
如果您无法影响父元素(例如在嵌套组件的情况下),您可以使用height: 100vh
,这将使其具有完整的窗口(=视图)高度;
这对我很有效。
<!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>
<!-- Maps Container -->
<div id="map_canvas" style="height:-webkit-fill-available;width:100px;"></div>
Gmap会将内联样式的位置设置为相对于div。使用以下代码覆盖它:
google.maps.event.addListener(map, 'tilesloaded', function(){
document.getElementById('maps').style.position = 'static';
document.getElementById('maps').style.background = 'none';
});
希望能够帮到你。
.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
是你页面上唯一的元素,就设置:body, html {
height: 100%;
width: 100%;
}
我为找到答案而苦苦挣扎。
你实际上不需要对正文大小做任何处理。你只需要从地图代码中删除内联样式:
<iframe width="425" height="350" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="https://maps.google.co.uk/maps?f=q&source=s_q&hl=en&geocode=&q=new+york&aq=&sll=53.546224,-2.106543&sspn=0.02453,0.084543&ie=UTF8&hq=&hnear=New+York,+United+States&t=m&z=10&iwloc=A&output=embed"></iframe><br /><small><a href="https://maps.google.co.uk/maps?f=q&source=embed&hl=en&geocode=&q=new+york&aq=&sll=53.546224,-2.106543&sspn=0.02453,0.084543&ie=UTF8&hq=&hnear=New+York,+United+States&t=m&z=10&iwloc=A" style="color:#0000FF;text-align:left">View Larger Map</a></small>
移除所有内联样式,添加类或ID,然后按照您喜欢的方式进行样式设置。
#content{background-color:#fff;border:solid 1px #a6a196;border-radius:4px;-moz-border-radius:4px;height:auto; overflow:hidden;}
- BBKing