I'm trying to get a Google Map to appear in a vanilla Bootstrap tab. I built a fiddle taken directly from the Bootstrap docs, with the Gmap script pretty much exactly like Google recommends doing it too.
I am dumping the map
object to console.dir and it has been initialized properly. In earlier projects I'd been able to display maps in tabs using the resize
function-- but it doesn't seem to work with Bootstrap.
Has anyone gotten this working?
Here's the generic jsfiddle-- http://jsfiddle.net/B4zLe/4/
EDIT: adding code
JAVASCRIPT:
var map;
jQuery(function($) {
$(document).ready(function() {
var latlng = new google.maps.LatLng(-34.397, 150.644);
var myOptions = {
zoom: 8,
center: latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
console.dir(map);
google.maps.event.trigger(map, 'resize');
$('a[href="#profile"]').on('shown', function(e) {
google.maps.event.trigger(map, 'resize');
});
});
});
HTML:
<div>
<div class="bs-docs-example">
<ul id="myTab" class="nav nav-tabs">
<li class="active"><a href="#home" data-toggle="tab">Home</a></li>
<li><a href="#profile" data-toggle="tab">Map</a></li>
</ul>
<div id="myTabContent" class="tab-content">
<div class="tab-pane fade in active" id="home">
<p>Raw denim you probably haven't heard of them jean shorts Austin. Nesciunt tofu stumptown aliqua, retro synth master cleanse. Mustache cliche tempor, williamsburg carles vegan helvetica. Reprehenderit butcher retro keffiyeh dreamcatcher synth. Cosby sweater eu banh mi, qui irure terry richardson ex squid. Aliquip placeat salvia cillum iphone. Seitan aliquip quis cardigan american apparel, butcher voluptate nisi qui.</p>
</div>
<div class="tab-pane fade" id="profile">
<div id="map_canvas"></div>
</div>
</div>
You're a little overoptimistic in how powerful the "resize" event is. From the documentation:
In other words, resize won't do anything fancy like size the map to the window, but will only size it to the container it's in - in this case, the "map_canvas" div. However, this div has no dimensions, so the map has zero size.
Since you added some listeners to ensure that the map will change to the size of it's parent, this is a pretty easy fix - just add some code to modify the parent div to the desired size, and the map will resize to it. For example:
will size it to 400x400. Here's an example, you should be able to change it to whatever you'd like.
use below script
Still having problems with Bootstrap 3.3.7, jquery 1.11.3 and tabs. Everything works fine until you try to put map into tab that on load is first hidden. Then you get map loaded and everything should work, but you see only gray (or any other color, not sure) area of map. You can do everything there but you cannot see the map itself. On drag inside the map, you may be able to see SOME things, but in general - blank map.
Option 1 - you are able to see the map when you resize your window for a moment. But there is downfall as well - as soon as you change tab, it disapperas again.
Option 2 - you can put map on first tab and be done with it.
Option 3 - you can just make a code to "resize" the map (like shown previously). BUT there I ran into the problem - that does not ALWAYS work. At least it did not work for me:
Pretty much any variation did not work. I managed to make it work on FIRST time tab click by using link ID not href comparision, but as soon as I clicked on another tab and back again, gray map again ... I was like - seriously!?
Finally, I came up with solution: on that specific tab click (by id, I recommend), it makes all the necessary things. That means following - each time you click somewhere else (other links or tabs) and back to map tab, it will reload that map (and therefor may be slow a bit - depends on internet), but at least I got map, not empty gray image of google maps.
Hope that helps someone else in the future.
I have the problem also with Google Maps with Bootstrap 3, but i solved the problem with tiny trick base on Andy B code :
well.. atleast its work for me :)
Resize the map when tabs is shown
http://jsfiddle.net/B4zLe/49/
Updated answer for Boostrap 3:
Note that you're selecting the link to the tab, not the tab itself.