javascript - 谷歌地图不加载单独的文件

标签 javascript html css

我在显示器上看不到谷歌地图,但是当我将所有代码放在一个 html 文件中并使用脚本标签时,它就可以工作了。

Index.html 文件是:

<!DOCTYPE html>
<html>
  <head>
    <title>Simple Map</title>
    <meta name="viewport" content="initial-scale=1.0">
    <meta charset="utf-8">
    <link rel="stylesheet" href="style.css">
    <script src="script.js"></script>
  </head>
  <body>
    <div id="map"></div>

    <script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyBIpWlWVDAqVEyW20SX6MfThL-iz9IWeQA&callback=initMap"
    ></script>
  </body>
</html>

Script.js 文件是:

function initMap() {

    var map;
            map = new google.maps.Map(document.getElementById('map'), 
            {
              center: {  lat: -34.397, 
                         lng: 150.644
                      },
              zoom: 8
            }
            );
          }

style.css 文件是:

* element that contains the map. */
  #map {
    height: 100%;
  }
  /* Optional: Makes the sample page fill the window. */
  html, body {
    height: 100%;
    margin: 0;
    padding: 0;
  }

最佳答案

#map { width:100%; height:200px;}

或者填满整个窗口:

#map { width:100%; min-height:100%;}

如果它是第一个具有 min-height 属性的 child 。附言。您也可以尝试添加 overflow:hidden;第二个例子。

关于javascript - 谷歌地图不加载单独的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44994442/

相关文章:

javascript - 如何在 Bootstrap 中对背景图像应用模糊效果

html - 链接没有悬停

javascript - 如果子数组的长度大于零,则显示对象

javascript - 想要了解有关延迟和异步的 W3C 规范?

javascript - 如何使用默认函数导出为 commonjs 模块创建类型?

javascript - 在 HTML 中制作一个向下滑动的文本区域

ios - 当我嵌入谷歌文档时,xcode 中的 iframe 宽度溢出

html - Bootstrap 2列右列覆盖背景图片

javascript - 如何在 jQuery 中使用变量 id 作为选择器

javascript - 手动设置 pan.x 和 pan.y 时对 svg pan 进行动画处理