【发布时间】:2018-07-31 04:30:09
【问题描述】:
当我将代码放入另一个没有任何其他内容的 html 文档中时,下面的代码实际上可以正常工作并显示具有正确位置和标记的地图(id="map" 的 div、带有 javascript 函数和 API 的脚本)。
现在的问题是,由于某种原因,当我在此 HTML 中使用具有工作 API 的 Google 地图的完全相同的代码时,地图根本不显示。
这是我的代码:
<html lang="en" dir="ltr">
<head>
<title>-</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="https://fonts.googleapis.com/css?family=Roboto+Condensed" rel="stylesheet">
<link href="https://fonts.googleapis.com/css?family=Roboto" rel="stylesheet">
<style>
/* Set the size of the div element that contains the map */
#map {
width: 100%; /* The width is the width of the web page */
max-width:450px;
}
</style>
<link rel="stylesheet" type="text/css" href="../main.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
...
<div class="row">
...
<div class="col-sm-6">
<div id="map"></div>
</div>
</div>
<script>
// Initialize and add the map
function initMap() {
// The location of company
var freshlocation = {lat: 1.337344, lng: 103.912835};
// The map, centered at company
var map = new google.maps.Map(
document.getElementById('map'), {zoom: 7, center: freshlocation});
// The marker, positioned at company
var marker = new google.maps.Marker({position: freshlocation, map: map});
}
</script>
<script async defer
src="https://maps.googleapis.com/maps/api/js?key=MYAPIKEY&callback=initMap">
</script>
...
</div>
</body>
</html>
我想知道它是否与head中的某些代码有关,例如导入字体或引导程序,或者地图div的位置,例如将地图放入容器或行类中。任何帮助将不胜感激。
【问题讨论】:
标签: javascript html google-maps