删除$(window).load()时Google Map消失 [英] Google Map disappear when delete $(window).load()
问题描述
HTML:
<div style="margin-left:25%">
<div id="map" style="width: 1000px; height: 500px"></div>
</div>
JavaScript:
JavaScript:
<script>
$(window).load(
function(){
var HoChiMinh = {lat: 10.7867246, lng: 106.6735853};
var map = new google.maps.Map(document.getElementById('map'), {
center: HoChiMinh,
zoom: 13,
});
});
</script>
<script src="https://maps.googleapis.com/maps/api/js?key={{ env('GOOGLE_MAPS_API_KEY') }}"
async defer></script>
我不知道$(window).load()
实际上在这里做什么.
I don't know what $(window).load()
actually do here.
当我删除行$(window).load()
时,我的Google地图没有加载.屏幕什么也没返回,直到我放回$(window).load()
,它才能再次工作.
When I delete the line $(window).load()
, my Google Map didn't load. The screen return nothing, until I put $(window).load()
back, it works again.
我需要删除$(window).load()
以便使用HTML代码中用户输入的某些数据.
I need to delete $(window).load()
for using some data from user input inside HTML code.
谢谢!
推荐答案
我仍然不太了解$(window).load()
在这里实际做什么.
I still don't really understand what $(window).load()
actually do here.
但是我刚刚找到了一个解决方案.当我将&callback=initMap
放入
But I've just found one solution. When I put &callback=initMap
in
<script src="https://maps.googleapis.com/maps/api/js?key={{ env('GOOGLE_MAPS_API_KEY') }}&callback=initMap"async defer></script>
并定义function()以使initMap()起作用.有用!现在,我的网站无需$(window).load()
即可加载地图.
And define the function() to function initMap(). It works! Now my website can load the map without $(window).load()
.
这篇关于删除$(window).load()时Google Map消失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!