Grey boxes appear in parts of embedded Google Map in modal box

I'm having a problem with embedding a Google Map via the v3 API in a modal box.

Grey boxes appear in the map canvas when the modal is shown. Resizing the browser window, bringing up Web Inspector, etc. makes all map tiles visible, i.e. it "force re-render" the map.

The parent element of the map element (section#map-modal, see code below) has display: none set in its CSS on page load. The JS modal code automatically sets display: block when the show button is clicked. If I temporarily remove display: none from the modal element, the map renders correctly on page refresh. Isn't the Google Map liking having a hidden parent element?

I'm using Twitter's Bootstrap modal jQuery plugin, and am controlling the modal itself with CSS. It's fixed positioned, have a pixel width, etc. Nothing unusual.

I've of course googled around for solutions, and many points to the Google API method of triggering the resize event:

google.maps.event.trigger(map, 'resize');

I've indeed done so, but to no avail.

Relevant code:

As you can see, I'me triggering the events at line 39.

(press the View larger map button at the bottom of the sidebar).


  • fagerhult.js
  • bootstrap-modal.js
  • master.css

I would deeply appreciate any help or extra pair of eyes in this, as I'm soon going mad over it.



Try using the modal event handlers. I think this is the most concise (and correct) way to ensure that you are resizing after the modal is shown without rewriting any plugins:

$('#map-modal').on('shown', function () {
  google.maps.event.trigger(map, 'resize');

UPDATE: I just realized that this solution still does not center the map properly, so I needed to add one more command:

$('#map-modal').on('shown', function () {
  google.maps.event.trigger(map, 'resize');
  map.setCenter(new google.maps.LatLng(42.3605336, -72.6362989));

I discovered that having display: none on the map's parent element (the modal) really messed things up. I changed it to visibility: hidden just in sheer desperation to see what would happen, and it worked!

I also modified the modal JS code to set the modal to visibility: visible/hidden when the modal is shown/hid. Otherwise it would get display: none/block set again, which would create the grey boxes again.


My solution for bootstrap-modal.js v 2.0

replace class "hide" for "invisible" in Modal div

<div id="map_modal" class="modal fade invisible">

function javascript

function open_map()

link html

<a href="javascript:open_map()">Open map</a>

For those using bootstrap, I had to remove "fade" from my "modal" class


<div class="modal fade" 


<div class="modal" ...

I had tried the previous solutions with calling google.maps.event.trigger(map, 'resize'); but figured that

.on("" ...

was never getting called. This issue seemed to point me to removing the fade.

This is not an optimal solution since the fade is actually very nice to have...


The problem is that if you do something like this:


is that your modal probably wasn't open (and displayed at the right size) when the resize event is triggered. To solve this:

  google.maps.event.trigger(map, 'resize');

Worked for me at least :)


I had this problem and I solved it doing a callback to the draw method. I think this happens because you draw your map erlier than showing the dialog.

I used this code to solve it (it wasn´t a modal window):

    var punto = new google.maps.LatLng(this.latitud, this.longitud);
    var myOptions = {zoom: 15, center: punto, mapTypeId: google.maps.MapTypeId.ROADMAP,mapTypeControl:false, draggable:false};
    var map = new google.maps.Map(document.getElementById('mapa'),  myOptions);
    var marker = new google.maps.Marker({
    map: map,
    title: this.nombre
google.maps.event.addListenerOnce(map, 'tilesloaded', function() {
     google.maps.event.addListenerOnce(map, 'tilesloaded', function() {
          google.maps.event.trigger(map, 'resize');


As far as I poked at it a bit on Chrome using JS live edit, delaying the execution of resize to wait for the modal animation might work :)


map rendered successfully without resize.

Why resize gives you proper map ?

Because browser paints the view again.

How to fix it?

To get a proper layout of the map in any panel which is triggered lately, map has to be painted after the panel is loaded.This can be achieved by (setTimeout) code as mentioned below. code objective is to trigger map resize event after 60 milli seconds.

setTimeout(function () {
        uiGmapIsReady.promise().then(function (maps) {
            google.maps.event.trigger(maps[0].map, 'resize');
            lat = -37;
            lon = 144;
            maps[0].map.panTo(new google.maps.LatLng(lat,lon));
            var marker = {
                coords: {
                    latitude: lat,
                    longitude: lon
            $ = [];
    }, 60);


Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.