嵌套在div标签中时,地图未显示在Google Maps JavaScript API v3上

51 javascript google-maps-api-3

我正在尝试将div显示map(<div id="map-canvas"></div>)的标记放在另一个内部div,但它不会以这种方式显示地图.它是CSS还是JavaScript问题?或者它只是API的工作方式?

这是我的嵌套代码div:

<!DOCTYPE html>
<html>
  <head>
    <title>Simple Map</title>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
    <meta charset="utf-8">
    <style>
        html, body, #map-canvas {
            margin: 0;
            padding: 0;
            height: 100%;
        }
    </style>
    <script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false">
    </script>
    <script>
        var map;
        function initialize() {
          var mapOptions = {
            zoom: 8,
            center: new google.maps.LatLng(-34.397, 150.644),
            mapTypeId: google.maps.MapTypeId.ROADMAP
          };
          map = new google.maps.Map(document.getElementById('map-canvas'),
              mapOptions);
        }

        google.maps.event.addDomListener(window, 'load', initialize);
    </script>
</head>
<body>
    <div> <!-- ommiting this div will show the map -->
        <div id="map-canvas"></div>
    </div>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

geo*_*zip 46

问题在于尺寸百分比.您没有定义父div(新版本)的大小,因此浏览器无法向Google Maps API报告大小.给包装器div一个特定的大小,如果可以确定其父级的大小,则可以使用百分比大小.

请参阅Mike Williams的Google Maps API v2教程中的此解释:

如果您尝试在地图div上使用style ="width:100%; height:100%",则会得到一个高度为零的地图div.这是因为div试图成为其大小的百分比<body>,但默认情况下<body>具有不确定的高度.

有几种方法可以确定屏幕的高度,并使用该像素数作为地图div的高度,但一个简单的替代方法是更改​​它<body>,使其高度为页面的100%.我们可以通过将style ="height:100%"应用于<body><html>.(我们必须对两者都这样做,否则 <body>尝试是文档高度的100%,并且默认值是不确定的高度.)

在css中添加100%大小的html和body

    html, body, #map-canvas {
        margin: 0;
        padding: 0;
        height: 100%;
        width: 100%;
    }
Run Code Online (Sandbox Code Playgroud)

将其内联添加到任何没有id的div:

<body>
  <div style="height:100%; width: 100%;"> 
    <div id="map-canvas"></div>
  </div>
</body>
Run Code Online (Sandbox Code Playgroud)


Nod*_*Dad 40

添加style="width:100%; height:100%;"到div看看它做了什么

不是#map_canvas主要的div

<body>
    <div style="height:100%; width:100%;">
         <div id="map-canvas"></div>
    </div>
</body> 
Run Code Online (Sandbox Code Playgroud)

这里有一些其他答案解释为什么这是必要的


小智 22

我解决了这个问题:

    <div id="map" style="width: 100%; height: 100%; position: absolute;">
                 <div id="map-canvas"></div>
     </div>
Run Code Online (Sandbox Code Playgroud)


Yer*_*lem 11

固定

给出div的初始高度,并在你的风格中指定高度百分比;

#map {
   height: 100%;
}

<div id="map" style="clear:both; height:200px;"></div> 
Run Code Online (Sandbox Code Playgroud)


Jos*_*f.B 9

在我的情况下,我得到灰色背景,结果是在地图选项中包含缩放值.是的,没有意义.


小智 5

向导

您是否尝试过设置额外 div 的高度和宽度,我知道在我正在处理的项目中,JS 不会在 div 中放置任何内容,除非我已经设置了高度和宽度。

我使用了你的代码并对高度和宽度进行了硬编码,它会显示给我,没有它就不会显示。

<body>
    <div style="height:500px; width:500px;"> <!-- ommiting the height and width will not show the map -->
         <div id="map-canvas"></div>
    </div>
</body> 
Run Code Online (Sandbox Code Playgroud)

我建议要么硬编码它,要么为 div 分配一个 ID,然后将它添加到你的 CSS 文件中。