2

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 is selected. The jQuery documentation here http://docs.jquery.com/UI/Tabs provides that:

For Google maps you can also resize the map once the tab is displayed like this:

$('#example').bind('tabsshow', function(event, ui) {
    if (ui.panel.id == "map-tab") {
        resizeMap();
    }
});

No matter what I try, the above code will not work for me.

The following code works insofar as it will display an alert when I select tab 9. For this reason, I know that at least I am correctly isolating the right trigger event. Here is the code:

$(function() {
    $( "#tabs" ).bind( "tabsselect", function(event, ui) { 
        if (ui.panel.id == "tabs-9") {
            alert("Alert is working");
        }                                         
    });
});

When I alter this code to include the recommended resizeMap(); command, it does not work. Instead, what it does is append the characters "#tabs-9" to the end of the url, and then it does not even select the correct tab 9. I cannot even see tab 9 when I use this code:

$(function() {
    $( "#tabs" ).bind( "tabsselect", function(event, ui) { 
        if (ui.panel.id == "tabs-9") {
            resizeMap();
        }                                         
    });
});

Finally, when I alter the code to include a different resize command "google.maps.event.trigger(map, 'resize');", it still does not work. Instead, it does allow me to select the correct tab 9, and it does not append the characters to the end of the URL as in the case above, but it will not resize the map. The map still display only partially complete and has gray bands on the right. Here is the code:

$(function() {
    $( "#tabs" ).bind( "tabsselect", function(event, ui) { 
        if (ui.panel.id == "tabs-9") {
           google.maps.event.trigger(map, 'resize');
        }                                         
    });
});

Any ideas what I can try next? I'm stumped. Thanks.

Kara
  • 6,115
  • 16
  • 50
  • 57
DanielAttard
  • 3,467
  • 9
  • 55
  • 104

5 Answers5

12

Add the following to your tabs constructor:

$(function() {
    $("#myTabs").tabs({
        show: function(e, ui) {
            if (ui.index == 0) {
                google.maps.event.trigger(myMap, "resize");
            }
        }
    });
});

You should test ui.index for the 0-based index of the panel that contains your Google map, and of course update references to myMap and #myTabs.

See the Events section of the Map object documentation for more information about dispatching a resize event trigger.

lsuarez
  • 4,952
  • 1
  • 29
  • 51
  • Thanks for the help. I'm trying to implement your suggestion but unfortunately it is still now working. – DanielAttard Jun 23 '11 at 15:06
  • This is working and generates an alert: $(function() { $("#tabs").tabs({ show: function(e, ui) { if (ui.index == 8) { alert("xxxx"); } } }); }); but this does not resize my map: $(function() { $("#tabs").tabs({ show: function(e, ui) { if (ui.index == 8) { google.maps.event.trigger("map-canvas", "resize"); } } }); }); – DanielAttard Jun 23 '11 at 15:08
  • You're passing "map-canvas" as a string. The map object is required. Note how `myMap` is not a string. You should be maintaining a reference to the object in a variable that contains the result returned from calling the map constructor. – lsuarez Jun 23 '11 at 15:11
  • sorry for the bad formatting in previous comment. should the reference to myMap be a reference to the map id as in:
    or is it a reference to: var map = new google.maps.Map(mapDiv, { center: new google.maps.LatLng(37.4419, -122.1419), zoom: 13, mapTypeId: google.maps.MapTypeId.ROADMAP
    – DanielAttard Jun 23 '11 at 15:12
  • thanks again, now i understand that the reference to myMap is not a string, but a reference to the object in a variable. Here is my initialize function: function initialize() { var mapDiv = document.getElementById('map-canvas'); var map = new google.maps.Map(mapDiv, { center: new google.maps.LatLng(37.4419, -122.1419), zoom: 13, mapTypeId: google.maps.MapTypeId.ROADMAP }); } – DanielAttard Jun 23 '11 at 15:28
  • Based on this, I guess the variable name I should be using is simply 'map' (without the quotes), but it still is not working for me. Instead what is happening is that it repositions the map to the upper left corner of the page, appends '#tabs-9' to the URL, but it does not resize and fix the map. It still appears with grey parts. – DanielAttard Jun 23 '11 at 15:28
  • Renaming you variable won't affect anything unfortunately. One name is like any other to JavaScript. I can't really tell you more than what I know about what it takes to trigger the Google Maps v3 resize event correctly. The jQuery UI `show` event is raised only after the panel element containing the content for the tab becomes visible, so at this point Google should be able to access browser attributes for the dimensions of its container. There may be other problems from your markup causing the visual inconsistency, or (more likely) the variable containing your map may no longer be in scope. – lsuarez Jun 23 '11 at 15:38
  • If you want to provide additional code to demonstrate the problem I suggest editing your original answer. – lsuarez Jun 23 '11 at 15:42
  • one last quick question - how can I determine if the variable containing my map is still in scope? – DanielAttard Jun 23 '11 at 16:07
  • `alert(!!variableName)` will return true if it's in scope, false otherwise. You should be using a browser debugging tool like the ones available from Chrome by pressing CTRL+SHIFT+J and clicking the Scripts button or Firebug for Firefox, or at the very least Firefox's error console which is also accessible with CTRL+SHIFT+J. They will tell you of any problems that occurred while executing your script. – lsuarez Jun 23 '11 at 16:30
4

I know that this topic is ancient but think that this may be useful to someone.

To avoid rendering problem with jQuery UI Tabs and Google Maps on some of that tabs, you can do following:

//global variables
var gMapsCentralLocation;   //some initial map center location
var mapCenter;              //we will use this to overide your initialy map center so we can "remember" initial viewport when we go back and forth trought tabs
var mapCenterFlag = false;  //indicate if we need to use new google maps viewpoint center, or use initial.
var map;                    //google map

$(document).ready(function () {
    $('#YourTabs').tabs({
        show: function (event, ui) {
            switch (ui.tab.hash) {
                case "#GoogleMapsTab":
                    {
                        preRenderGoogleMaps();
                    }
                    break;
                default:
                    {
                        //to save previous map center when user select some other tab
                        if (map) {
                            mapCenter = map.getCenter();
                        }
                    }
                    break;
            }
        }
    });
    drawRegionOnMap();
});

function preRenderGoogleMaps(){
    //fix rendering issues
    google.maps.event.trigger(map, 'resize');

    if (!mapCenterFlag) {
        map.setCenter(gMapsCentralLocation);
        mapCenterFlag = true;
    } else {
        map.setCenter(mapCenter);
    }
}

function drawRegionOnMap() {
    map = null;    

    var gMapsOptions = {
        zoom: 8,
        center: gMapsCentralLocation,
        //POSSIBLE MAP TYPES (ROADMAP, SATELLITE, HYBRID, TERRAIN)
        mapTypeId: google.maps.MapTypeId.ROADMAP
    };

    map = new google.maps.Map(document.getElementById("map_canvas"), gMapsOptions);
}
ShP
  • 1,143
  • 1
  • 12
  • 41
2

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!

mossman
  • 87
  • 2
  • 9
0

First, go to the file where you put the tab template, search for the <li> tag that load your map tab and put this inside:

<li onclick="reloadmap()">

And in the map script right after the

google.maps.event.addDomListener(window, 'load', initialize);

put this:

function reloadmap(){
  //when you resize the map, you lose your zoom and your center
  //so you need to get them again here
    z = map.getZoom();
    c = map.getCenter();
    google.maps.event.trigger(map, 'resize');
  //and set them again here
    map.setZoom(z);
    map.setCenter(c);
}
Jeffrey Bosboom
  • 13,313
  • 16
  • 79
  • 92
Número Perdido
  • 31
  • 2
  • 2
  • 6
0

i resolved by just css

map-tab {width: 100%; height: 300px;float: left;display: block !important;margin: 0px; padding: 0px;}

Then hide the hidden tab :

div[aria-hidden="true"]{position: absolute !important; left: -9999999px;}}

Community
  • 1
  • 1