Alex Rivera | Logout

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

Asked 2012-01-10T23:49:51.557
18

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: https://gist.github.com/1591488

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

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

Files:

  • fagerhult.js
  • fagerhult.map.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.

Edit
Report

2 Answers

7

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.

answered 2012-01-12T18:00:20.850
2

The problem is that if you do something like this:

$("#mapModalLink").click(function(){
  google.maps.event.trigger(map,"resize");
});

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

$("#mapModalLink").click(function(){
  $("#mapModal").show();
  google.maps.event.trigger(map, 'resize');
});

Worked for me at least :)

answered 2012-04-13T06:48:31.660

Your Answer