我在gmaps-api-issues跟踪器https://issuetracker.google.com/issues/38295566上看到了之前提到的问题,但是我看不到任何解决方案。
调用Google Maps JavaScript API时,它返回的common.js文件有一行调用:
b.j.getElementsByTagName("HEAD")[0].appendChild(f);
这将导致错误“未捕获(承诺)TypeError:无法读取在新_.Xq(common.js:114)上未定义的属性'appendChild'
common.js文件中的所有其他引用都使用:
getElementsByTagName("head")
从短期来看,在修复common.js错误之前,我可以使用哪些解决方法来解决此问题?
答案 0 :(得分:1)
the issue you reference中有一种解决方法:
JavaScript插入的另一个HEAD元素可成功显示地图,而不会报告任何控制台错误。
使用JavaScript插入其他HEAD元素。
<style>
#map {
height: 100%;
}
html,
body {
height: 100%;
margin: 0;
padding: 0;
}
HEAD {
display: none;
}
</style>
<script>
document.documentElement.appendChild(document.createElementNS('http://www.w3.org/1999/xhtml', 'HEAD'));
</script>
</head>
<body>
<div id="map"></div>
<script>
var map;
function initMap() {
map = new google.maps.Map(document.getElementById('map'), {
center: {
lat: -34.397,
lng: 150.644
},
zoom: 8
});
}
</script>
<script src="https://maps.googleapis.com/maps/api/js?callback=initMap" async="async" defer="defer"></script>