我在显示器上看不到谷歌地图,但是当我将所有代码放在一个 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/