问题描述
我在通过v3 API在模式框中嵌入Google地图时遇到问题。
灰色框出现在地图画布上显示模式。显示浏览器窗口的大小,调出Web Inspector等,使得所有地图块可见,即它强制重新渲染地图。
map元素的父元素( section#map-modal
,见下面的代码)有 display:none
在页面加载时在CSS中设置。当显示按钮被点击时,JS模式代码自动设置 display:block
。如果我暂时从模式元素中删除 display:none
,则地图在页面刷新上正确呈现。是不是Google Map喜欢隐藏父元素?
我使用Twitter的Bootstrap模式jQuery插件,并使用CSS控制模式本身。它是固定的,具有像素宽度等。没有什么不寻常的。
我当然会搜索解决方案,并且很多都指向触发 resize
的Google API方法。事件:
google.maps.event.trigger(map,'resize');
我确实这样做了,但没有用。
相关代码:
正如你所看到的,我在第39行触发了这些事件。
(按侧边栏底部的查看大图按钮)。
文件:
fagerhult.js
fagerhult.map.js
bootstrap-modal.js
master.css
我非常感谢任何帮助或额外的一双眼睛,很快就会生气。
尝试使用模态事件处理程序。我认为这是最简洁(正确)的方式,以确保在显示模式之后重新调整大小而不重写任何插件:
<$ c $ (''shown',function(){
google.maps.event.trigger(map,'resize');
})$ b $('#map-modal' b
更新:我刚刚意识到这个解决方案仍然没有正确地将地图居中,所以我需要添加一个命令:
$('#map-modal')。on('shown',function(){
google .maps.event.trigger(map,'resize');
map.setCenter(new google.maps.LatLng(42.3605336,-72.6362989));
})
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.
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));
})
这篇关于灰色框出现在嵌入式Google地图的部分模式框中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!