【发布时间】:2021-08-08 01:11:27
【问题描述】:
“地图:预期为 Element 类型的 mapDiv,但被传递为 null。”
-div的高度在css中设置。
Javascript (googleMaps.js):
function initMap() {
try {
const sportHall2 = { lat: 51.18310959584043, lng:
4.38829092697164 };
const map2 = new google.maps.Map(document.getElementById("map-2"),
{
zoom: 13,
center: sportHall2,
disableDefaultUI: true,
zoomControl: true,
fullscreenControl: true,
streetViewControl: true,
});
const map2b = new google.maps.Map(document.getElementById("map-2b"), {
zoom: 13,
center: sportHall2,
disableDefaultUI: true,
zoomControl: true,
fullscreenControl: true,
streetViewControl: true,
});
} catch (error) {
console.log(error);
}
}
HTML 第 1 页:
<script src="/scripts/googleMaps.js"></script>
<div class="grid-card">
<h3>Pius X-instituut</h3>
<h5>Bekijk de sporthal hier.</h5>
<p>Adres: Hof Van Tichelen 28 2020 Antwerpen</p>
<div id="map-2"></div>
</div>
<script src="/scripts/googleMaps.js"></script>
<script src="https://maps.googleapis.com/maps/api/js?
key=AIzaSyDsZ0rx4aEk6eegfEHbfAl2Z_IA8iY6ho4&callback=
initMap&libraries=&v=
weekly" async></script>
HTML 第 2 页(发生错误,地图不会显示在此页面上):
<script src="/scripts/googleMaps.js"></script>
<div class="grid-card map-2b-container">
<h3>Pius X-instituut</h3>
<h5>Bekijk de sporthal hier.</h5>
<p>Adres: Hof Van Tichelen 28 2020 Antwerpen</p>
<div id="map-2b"></div>
</div>
<script src="/scripts/googleMaps.js"></script>
<script src="https://maps.googleapis.com/maps/api/js?
key=AIzaSyDsZ0rx4aEk6eegfEHbfAl2Z_IA8iY6ho4&callback=
initMap&libraries=&v=weekly" async></script>
CSS:
#map-2{
height: 300px;
width: 100%;
}
#map-2b{
height: 300px;
width: 100%;
}
错误: Se {message: "Map: 预期 mapDiv 类型为 Element 但已通过 null.", name: "InvalidValueError", stack: "Error↵ at new Se (https://maps.googleapis.com/m… 6ho4&callback=initMap&libraries=&v=weekly:156:128"}
【问题讨论】:
-
你在使用打字稿吗?
-
请提供一个minimal reproducible example 来证明您的问题。
-
我正在使用 JavaScript。 @geocodezip 谢谢你的建议,我相信现在应该是一个reprex/mcve/mwe。
-
我希望您在两个页面上都会遇到该错误(每个页面都尝试访问两个 div,但每个页面只有一个......)。如果 div 存在,也许只能访问它;或在两个页面上为地图 div 使用相同的名称。
-
有道理,我为地图 div 尝试了相同的名称,但没有任何区别。我还尝试在运行同样不起作用的功能之前检查 div 是否存在。之后,我尝试在两个 HTML 页面上同时使用地图 div 元素,并且在 HTML 页面 1 上仍然只显示 map-2,而在 HTML 页面 2 上没有加载任何地图。错误保持不变。
标签: javascript html css google-maps-api-3