Grey boxes appear in parts of embedded Google Map

2019-01-08 12:55发布

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.

9条回答
虎瘦雄心在
2楼-- · 2019-01-08 13:58

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 :)

https://gist.github.com/1592330#L107

查看更多
放荡不羁爱自由
3楼-- · 2019-01-08 13:58

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 = {
                id: Date.now(),
                coords: {
                    latitude: lat,
                    longitude: lon
                }
            };
            $scope.map.markers = [];
            $scope.map.markers.push(marker);
            console.log($scope.map.markers);
        });
    }, 60);
查看更多
趁早两清
4楼-- · 2019-01-08 14:00

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

From

<div class="modal fade" 

to

<div class="modal" ...

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

.on("shown.bs.modal" ...

was never getting called. This issue seemed to point me to removing the fade.
https://github.com/twbs/bootstrap/issues/11793

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

查看更多
登录 后发表回答