Google Maps API v3, jQuery UI Tabs, map not resizing

后端 未结 5 1806
闹比i
闹比i 2020-12-10 09:34

I am using Google Maps API v3, being displayed on a jQuery UI Tab. I have been struggling with triggering my google map to resize correctly when the tab displaying the map

5条回答
  •  忘掉有多难
    2020-12-10 10:10

    I managed to solve this by initialising the map when the map tab is shown and by also editing some css (the css is more than likely a theme related issue).

    /* Initialise jQuery UI Tabs */
    jQuery(function() {
        jQuery( "#tabs" ).tabs({
          fx: {
            opacity: 'toggle',
            duration: 300
          }
        });
    });
    
    /* Redraw the Google Map everytime tab-4 is shown/clicked */
    jQuery(function() {
                jQuery( "#tabs" ).bind( "tabsshow", function(event, ui) { 
                    if (ui.panel.id == "tabs-4") {
                        var mapOptions = {
                          center: myLatlng,
                          zoom: 13,
                          mapTypeId: google.maps.MapTypeId.ROADMAP
                        };
                        var map = new google.maps.Map(document.getElementById("map"), mapOptions);
                    }                                         
                });
    });
    

    And then add some CSS to fix the map controls if they are out of place etc.

    #map img { max-width:none; }
    

    if max-width of all images is set to 100% you will get problems.

    Hope this helps!

提交回复
热议问题