【问题标题】:Google maps API with tabs needs double click when returning to the map tab返回地图选项卡时,带有选项卡的 Google 地图 API 需要双击
【发布时间】:2017-10-26 01:14:04
【问题描述】:

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

我有两个选项卡,一个带有地图。当我打开页面时,地图加载,一切正常。但是,如果我转到另一个选项卡并返回,则地图不会加载。如果我双击地图,它会正常加载。 我一直在阅读一些帖子,例如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 地图:

<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.

【问题讨论】:

  • 你试过google.maps.event.trigger(map, 'resize'); ,当你的标签改变时?
  • 添加了回答的解决方案,当您的第一个标签被点击/激活时,我们需要调整地图大小
  • 请将解决方案放入回答帖子中,而不是您的问题中。

标签: javascript jquery google-maps google-maps-api-3


【解决方案1】:

激活选项卡时尝试调整地图大小:

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

【讨论】:

  • 是的,而且效果很好!但是我对选项卡的顺序有疑问:如果地图选项卡是第二个,如果您不双击则不会加载。如果是第一个,它会加载,但中心有问题。我正在努力解决它……
【解决方案2】:

试试这个:

<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>

【讨论】:

  • 谢谢你,这行得通!不幸的是,当我尝试在我的网站上实现它时,它不起作用,所以我需要检查更多的东西......无论如何,谢谢!
  • 我让它在网站上工作:奇怪的解决方案,但它工作 ;)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-03-31
  • 2015-01-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多