模态框中嵌入的 Google 地图部分出现灰色框
我在通过 v3 API 在模式框中嵌入 Google 地图时遇到问题。
当显示模式时,地图画布中会出现灰色框。调整浏览器窗口大小、调出 Web 检查器等会使所有地图图块可见,即“强制重新渲染”地图。
地图元素的父元素(section#map-modal
,请参阅下面的代码)在页面加载时在其 CSS 中设置了 display: none
。当点击show按钮时,JS模态代码会自动设置display: block
。如果我暂时从模式元素中删除 display: none
,则地图会在页面刷新时正确呈现。谷歌地图不喜欢有一个隐藏的父元素吗?
我正在使用 Twitter 的 Bootstrap 模态 jQuery 插件,并使用 CSS 控制模态本身。它的位置是固定的,有像素宽度等。没有什么不寻常的。
我当然已经在谷歌上搜索过解决方案,并且很多都指向触发 resize
事件的 Google API 方法:
google.maps.event.trigger(map, 'resize');
我确实这样做了,但没有效果。
相关代码:https://gist.github.com/1591488
可以看到,我正在第 39 行触发事件。
(按侧边栏底部的查看更大的地图按钮)。
文件:
fagerhult.js
fagerhult.map.js
bootstrap-modal.js
master.css
我将非常感激任何帮助或额外的眼睛在这,因为我很快就会对此发疯。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
尝试使用模式事件处理程序。我认为这是确保在显示模式后调整大小而不重写任何插件的最简洁(也是正确)的方法:
更新:我刚刚意识到这个解决方案仍然没有正确居中地图,所以我需要添加一个更多命令:
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:
UPDATE: I just realized that this solution still does not center the map properly, so I needed to add one more command:
我发现在地图的父元素(模态)上使用
display: none
确实把事情搞砸了。我将其更改为visibility:hidden
只是出于纯粹的绝望,想看看会发生什么,结果成功了!我还修改了模态 JS 代码,以在模态显示/隐藏时将模态设置为
visibility:visible/hidden
。否则,它将再次设置display: none/block
,这将再次创建灰色框。I discovered that having
display: none
on the map's parent element (the modal) really messed things up. I changed it tovisibility: 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 getdisplay: none/block
set again, which would create the grey boxes again.我的 bootstrap-modal.js v 2.0 解决方案
将 Modal div
函数 javascript
link html中的类“hide”替换为“invisible”
My solution for bootstrap-modal.js v 2.0
replace class "hide" for "invisible" in Modal div
function javascript
link html
对于那些使用引导程序的人,我必须从我的“模态”类中删除“淡入淡出”
从
到
我尝试了以前的解决方案,调用 google.maps.event.trigger(map, 'resize');但我想那
永远不会被叫到。这个问题似乎提示我消除淡入淡出。
https://github.com/twbs/bootstrap/issues/11793
这不是最佳解决方案,因为淡入淡出实际上非常好......
For those using bootstrap, I had to remove "fade" from my "modal" class
From
to
I had tried the previous solutions with calling google.maps.event.trigger(map, 'resize'); but figured that
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...
问题是,如果您执行以下操作:
触发调整大小事件时,您的模式可能未打开(并以正确的大小显示)。为了解决这个问题:
至少对我有用:)
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:
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):
据我使用 JS 实时编辑在 Chrome 上进行了一些研究,延迟执行
resize
以等待模态动画可能会起作用:)https://gist.github.com/1592330#L107
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
地图渲染成功,无需调整大小。
为什么调整大小会给你正确的地图?
因为浏览器会再次绘制视图。
如何修复它?
为了在最近触发的任何面板中获得正确的地图布局,必须在加载面板后绘制地图。这可以通过下面提到的 (setTimeout) 代码来实现。
代码目标是在 60 毫秒后触发地图调整大小事件。
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.