灰色框出现在模态框中嵌入的 Google 地图的部分中 [英] Grey boxes appear in parts of embedded Google Map in modal box

查看:24
本文介绍了灰色框出现在模态框中嵌入的 Google 地图的部分中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在通过 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模态代码在点击show按钮时自动设置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));
})

这篇关于灰色框出现在模态框中嵌入的 Google 地图的部分中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆