Showing a Google Map in a modal created with Twitter Bootstrap

Google Maps indeed displays “Grey” area’s when it’s placed inside a dynamic element (for example: One that resizes, fades etc.). You’re right about triggering the “resize” function, which should be called once the animation is complete (the shown.bs.modal event in Bootstrap 3 or the shown event in Bootstrap 2):

$("#myModal").on("shown.bs.modal", function () {
    google.maps.event.trigger(map, "resize");
});

In Bootstrap 2, you will do:

$('#myModal').on('shown', function () {
    google.maps.event.trigger(map, "resize");
});

(where map is the variable name of your map (see Google Maps documentation for further details), and #myModal the ID from your element).

UPDATE 2018-05-22

With a new renderer release in version 3.32 of Maps JavaScript API the resize event is no longer a part of Map class.

The documentation states

When the map is resized, the map center is fixed

  • The full-screen control now preserves center.

  • There is no longer any need to trigger the resize event manually.

source: https://developers.google.com/maps/documentation/javascript/new-renderer

google.maps.event.trigger(map, "resize"); doesn’t have any effect starting from version 3.32

Leave a Comment