灰色框出现在模态框中嵌入的谷歌地图的部分中
我在通过 v3 API 在模态框中嵌入 Google 地图时遇到问题.
I'm having a problem with embedding a Google Map via the v3 API in a modal box.
显示模式时地图画布中会出现灰色框. 调整浏览器窗口大小、打开 Web Inspector 等使所有地图图块可见,即它强制重新渲染"地图.
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.
地图元素的父元素(section#map-modal
,参见下面的代码)在页面加载时的 CSS 中设置了 display: none
.当点击显示按钮时,JS 模态代码会自动设置 display: block
.如果我暂时从模态元素中删除 display: none
,则地图会在页面刷新时正确呈现.谷歌地图不喜欢有一个隐藏的父元素吗?
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?
我正在使用 Twitter 的 Bootstrap 模态 jQuery 插件,并使用 CSS 控制模态本身.它是固定位置的,有像素宽度等.没什么不寻常的.
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.
我当然在谷歌上搜索过解决方案,并且很多点都指向了触发 resize
事件的 Google API 方法:
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.
相关代码:https://gist.github.com/1591488
如你所见,我在第 39 行触发了事件.
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
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.
推荐答案
尝试使用模态事件处理程序.我认为这是确保在显示模式后调整大小而不重写任何插件的最简洁(和正确)的方法:
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));
})
相关文章