gpt4 book ai didi

javascript - 返回 map 选项卡时,带有选项卡的 Google map API 需要双击

转载 作者:行者123 更新时间:2023-11-29 11:02:46 24 4
gpt4 key购买 nike

我是 Javascript 的初学者,我正在尝试处理 Google map 的 API。

我有两个选项卡,一个带有 map 。当我打开页面时, map 加载,一切正常。但如果我转到另一个选项卡,然后返回,则 map 不会加载。如果我双击 map ,它会正常加载。我一直在阅读一些帖子,比如 Google maps in hidden div , 但没有结果。

我的 HTML 是:

<head>
<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyAxuujeWPQQygBjre-AD3U8p_tLNZgdw-4" async defer></script>
</head>
<body onload="initMap()">
<main id="main">
<section id="tabs">
<ul>
<li><a href="#tab1" onclick="initMap()">Map tab</a></li>
<li><a href="#tab2">Tab for test</a></li>
</ul>
<section id="tab1">
<section style="position: relative; width: 500px; padding-top: 500px; overflow: hidden;">
<div style="position: absolute; top: 0px; width: 500px; height: 500px;">
<div id="map" style="height:500px;"></div>
</div>
</section>
</section>
<section id="tab2">
<div>
Tab for testing
</div>
</section>
</section>
</main>
<script>

</script>
</body>

还有我的脚本,用于 Google map :

<script>
var map;
function initMap() {
var styleArray = [
{
featureType: 'all',
stylers: [
{ saturation: -80 }
]
},{
featureType: 'road.arterial',
elementType: 'geometry',
stylers: [
{ hue: '#00ffee' },
{ saturation: 50 }
]
},{
featureType: 'poi.business',
elementType: 'labels',
stylers: [
{ visibility: 'off' }
]
}];
// Coordinates
var coord = {lat: <?php echo '40.689095'; ?>, lng: <?php echo '-74.044640'; ?>};
// Icon
var imagen2 = "img/img_web/marker2.png";
//Create map
map = new google.maps.Map(document.getElementById('map'), {
center: coord,
zoom: 13,
styles: styleArray
});
var markerZ = new google.maps.Marker({icon: imagen2, map: map, position: coord});
};
</script>

标签脚本:

<script>
$(function() {
$( "#tabs" ).tabs({
activate:function(event,ui){
localStorage.setItem("lastTab",ui.newTab.index() );
},
active: localStorage.getItem("lastTab") || 0
});
});
</script>

有人知道我做错了什么吗?

N.

最佳答案

试试这个:

<script>
$(function() {
$( "#tabs" ).tabs({
activate:function(event,ui){
google.maps.event.trigger(map, 'resize');
map.setZoom( map.getZoom() );
localStorage.setItem("lastTab",ui.newTab.index() );
},
active: localStorage.getItem("lastTab") || 0
});
});
</script>

关于javascript - 返回 map 选项卡时,带有选项卡的 Google map API 需要双击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44179615/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com