views:

6949

answers:

12

There are a number of problems, which seem to be fairly well-known, when using the Google Maps API to render a map within a jQuery UI tab. I've seen SO questions posted about similar issues (here and here, for example) but the solutions there only seem to work for v2 of the Maps API. Other references I checked out are here and here, along with pretty much everything I could dig up through Googling.

I've been trying to stuff a map (using v3 of the API) into a jQuery tab with mixed results. I'm using the latest versions of everything (currently jQuery 1.3.2, jQuery UI 1.7.2, don't know about Maps).

This is the markup & javascript:

<body>
    <div id="dashtabs">
        <span class="logout">
            <a href="go away">Log out</a>
        </span>
        <!-- tabs -->
        <ul class="dashtabNavigation">
            <li><a href="#first_tab" >First</a></li>
            <li><a href="#second_tab" >Second</a></li>
            <li><a href="#map_tab" >Map</a></li>
        </ul>

        <!--  tab containers -->
        <div id="first_tab">This is my first tab</div>
        <div id="second_tab">This is my second tab</div>
        <div id="map_tab">
             <div id="map_canvas"></div>
        </div>
    </div>
</body>

and

$(document).ready(function() {
    var map = null;
    $('#dashtabs').tabs();
    $('#dashtabs').bind('tabsshow', function(event, ui) {
        if (ui.panel.id == 'map_tab' && !map)
        {
            map = initializeMap();
            google.maps.event.trigger(map, 'resize');
        }
    });
});

function initializeMap() {
    // Just some canned map for now
    var latlng = new google.maps.LatLng(-34.397, 150.644);
    var myOptions = {
        zoom: 8,
        center: latlng,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    };
    return new google.maps.Map($('#map_canvas')[0], myOptions);
}

And here's what I've found that does/doesn't work (for Maps API v3):

  • Using the off-left technique as described in the jQuery UI Tabs documentation (and in the answers to the two questions I linked) doesn't work at all. In fact, the best-functioning code uses the CSS .ui-tabs .ui-tabs-hide { display: none; } instead.
  • The only way to get a map to display in a tab at all is to set the CSS width and height of #map_canvas to be absolute values. Changing the width and height to auto or 100% causes the map to not display at all, even if it's already been successfully rendered (using absolute width and height).
  • I couldn't find it documented anywhere outside of the Maps API, but map.checkResize() won't work anymore. Instead, you have to fire a resize event by calling google.maps.event.trigger(map, 'resize').
  • If the map is not initialized inside of a function bound to a tabsshow event, the map itself is rendered correctly but the controls are not - most are just plain missing.

So, here are my questions:

  • Does anyone else have experience accomplishing this same feat? If so, how did you figure out what would actually work, since the documented tricks don't work for Maps API v3?
  • What about loading tab content using Ajax as per the jQuery UI docs? I haven't had a chance to play around with it but my guess is that it's going to break Maps even more. What are the chances of getting it to work (or is it not worth trying)?
  • How do I make the map fill the largest possible area? I'd like it to fill the tab and adapt to page resizes, much in the way that it's done over at maps.google.com. But, as I said, I appear to be stuck with applying only absolute width and height CSS to the map div.

Sorry if this was long-winded but this might be the only documentation for Maps API v3 + jQuery tabs. Cheers!

A: 

I found the google maps group to be very helpful with I did jquery + maps. I did not use the tabs or jquery ui though.

Jay
All of the problems, as far as I can tell, are a direct result of putting the map into a jQuery UI tab. Though people in the maps group might have experience with this, it didn't seem like the place for this question. I might end up asking around.
Matt Ball
bummer. They were very helpful with the API part but couldn't help you on the jquery side.
Jay
A: 

Any Luck with this since you posted? Ive had the exact same problem for the past week!!

google.maps.event.trigger(map, 'resize') doesnt seem to be doing it for me,

Cheers,

Stuart

Stuart C
I've more or less solved all the problems I mentioned above (though I switched to API v2). Any specific ones you need help on?
Matt Ball
A: 
Dave
A: 

You can call

map.fitBounds(bounds)

that will do the refresh

Juan arce
A: 

I had this problem too, I was loading the maps through ajax.

It seems the problem with percentage was to do with the panel not having any set size (the one containing the loaded markup).

Using the following code when creating the tabs helped me a lot:

$("#mainTabs").tabs({'show': function(event, ui){
$(ui.panel).attr('style', 'width:100%;height:100%;');
return true;
}});

Andy
+1  A: 

Be sure that the code that initializes your map is called BEFORE the snippet that initializes your tabs.

The only annoyances I had were that a click on the map tab made the browser jump to focus on the map and that it broke out of the default border that jQuery puts around the main tabs container. I added a return false onclick call to the map tab's a tag to handle the first and a simple border-size: 0 on the main tabs div to handle the second.

All of this worked for me.

Good luck!

Vaughn
+4  A: 

Actually scratch my answer above. The jQueryUI website has the answer and here it is:

Why does...

...my slider, Google Map, sIFR etc. not work when placed in a hidden (inactive) tab?

Any component that requires some dimensional computation for its initialization won't work in a hidden tab, because the tab panel itself is hidden via display: none so that any elements inside won't report their actual width and height (0 in most browsers).

There's an easy workaround. Use the off-left technique for hiding inactive tab panels. E.g. in your style sheet replace the rule for the class selector ".ui-tabs .ui-tabs-hide" with

.ui-tabs .ui-tabs-hide {
    position: absolute;
    left: -10000px;
}

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

resizeMap() will call Google Maps' checkResize() on the particular map.

jeffkee
Just edit your old answer, please. On top of that, I addressed the fact that the off-left technique suggested by the jQueryUI site doesn't work in this case.
Matt Ball
My apologies - I didn't know I could edit an existing comment. Being on blogs all the time, I'm just in the habit of thinking "once I spit it, I can't take it back."All I can tell you is that I have maps/streetviews working in the tab perfectly on this page:http://jackying.brixwork.com/showlisting.php/63/1701-388-Drake-Street-False-Creek-North-Vancouver-The best I can do is show you the code, perhaps you can make comparisons and see what's missing/different.. my Streetview did NOT work for serveral days until I found the solution I posted.
jeffkee
A: 

Hey guys, this code fixes the error but in IE not working ... after searching and searching, I discovered that we must add the following line and everything works to perfection:

< !--[if IE]> < style type="text/css"> .ui-tabs .ui-tabs-hide { position: relative; } < /style> < ![endif]-->

Andrés Sepúlveda
A: 

.ui-tabs .ui-tabs-hide { position: absolute !important; left: -10000px !important; display:block !important; } just redefine the css class for hidden tab.

memberyh
A: 

I had the same problem. Couldn't get the suggestions about checkRezise() or ResizeMap() to work. For now I'm doing:

$("#map_canvas").css("height", $(window).height()*4/5);

This gives a nice big map window but no scrollbar to the right.

David K
A: 

I load the maps after the tab has been shown.

This is hacky but it worked for me. Simply store the url of the map iframe inside the iframes rel attribute like so:

<iframe width="490" height="300" rel="http://maps.google.com/maps?f=q&amp;amp;source=s..."&gt;&lt;/iframe&gt;

This event will set the iframe src attribute to the url inside the rel.

        $("#tabs").tabs({
            show:function(event, ui) {
                var rel = $(ui.panel).find('iframe').attr('rel');
                $(ui.panel).find('iframe').attr('src',rel);
            }
        });
Keyo
A: 
google.maps.event.trigger(map, 'resize');
map.setZoom( map.getZoom() );

is suggested by http://code.google.com/p/gmaps-api-issues/issues/detail?id=1448 as a v3 substitute for v2's checkResize().

Blech - bad google, no cookie.

Anon