返回地图标签时,Google地图API与标签需要双击

时间:2017-05-25 11:36:27

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

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

有人知道我做错了吗?

<磷>氮

2 个答案:

答案 0 :(得分:4)

试试这个:

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

答案 1 :(得分:0)

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

$(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
    });
});