我制作了一个可以改变大小的谷歌地图,但它在 Firefox 中不起作用
I made a google map that can change sizes but it does not work in Firefox
我的代码在Google Chrome和IE中有效,但在Firefox 12中不起作用。 有人知道我如何在 Firefox 12 中使其工作吗?
法典:
...
/* START FULLSCREEN BUTTON */
// Globals
var map_size = 'small';
var map_position= 'inherit';
var map_top = 0;
var map_left = 0;
var map_width = 0;
var map_height = 0;
var map_zIndex = 0;
...
// when button clicked
if(map_size == 'small') {
map_size = 'large';
map_position= $('div.map').css('position');
map_top = $('div.map').css('top');
map_left = $('div.map').css('left');
map_width = $('div.map').css('width');
map_height = $('div.map').css('height');
map_zIndex = $('div.map').css('z-index');
$('#fullscreen').attr('src','/assets/images/restore.png');
$('#enlarge').html('Shrink Map');
controlUI.title = 'Click to make map smaller';
$('div.map').css('position', "fixed");
$('div.map').css('top', "10%");
$('div.map').css('left', "10%");
$('div.map').css('width', "80%");
$('div.map').css('height', "80%");
$('div.map').css('z-index', 10000);
google.maps.event.trigger(map,'resize');
map.panToBounds(bounds);
map.fitBounds(bounds);
} else {
map_size = 'small';
$('div.map').css('position', map_position);
$('div.map').css('top', map_top);
$('div.map').css('left', map_left);
$('div.map').css('width', map_width);
$('div.map').css('height', map_height);
$('div.map').css('z-index', map_zIndex);
$('#fullscreen').attr('src','/assets/images/fullscreen.png');
$('#enlarge').html('Enlarge Map');
controlUI.title = 'Click to make map bigger';
google.maps.event.trigger(map,'resize');
map.panToBounds(bounds);
map.fitBounds(bounds);
}
}
...
这是一个活生生的例子 http://www.helloflight.com/flight/dlh410.cfm
更新 - 请注意,实时示例使用下面的 setTimeout 解决方案,因此在 Firefox 12 中有效,但没有 setTimeout 解决方案在 Firefox 12 中不起作用
如您所见,它可以在最新版本的 Safari、Google Chrome 和 IE 中运行,但在 Firefox 中,当地图div 放大时,地图磁贴仍然是原始div 尺寸的大小,并且不会展开以填充新的div 尺寸。
我能说的最好的是,Firefox 的 Javascript 引擎有一个作弊,它试图并行执行函数中的行,它认为代码不需要按顺序运行,当它猜对时,这会加快速度,但在这里,因为 $('div.map').css('width', "80%"); 和 $('div.map').css('height',"80%"); 在 Firefox 上没有阻塞(与其他浏览器不同), google.maps.event.trigger(map,'resize');在 $('div.map').css('width',"80%")之前完成;和 $('div.map').css('height',"80%");因此,谷歌地图对象使用旧尺寸调整大小,修复:取代:
google.maps.event.trigger(map,'resize');
map.panToBounds(bounds);
map.fitBounds(bounds);
跟:
setTimeout(resizeMap,250);
并创建函数:
function resizeMap() {
google.maps.event.trigger(map,'resize');
map.panToBounds(bounds);
map.fitBounds(bounds);
}
250 毫秒的延迟应该足以让 Google 地图 v3 对象知道调整大小的div 的正确尺寸。 为了使一切渲染更流畅,更快,您可以检测浏览器,如果浏览器是Firefox,则通过setTimeout调用函数,如果不是Firefox,则只需调用函数。
- 谷歌地图不是以HTML显示,而是在JS Fiddle上工作
- 嵌入的自定义谷歌地图不会在没有刷新的情况下显示
- 动态坐标谷歌地图不会't负载
- 谷歌地图不正确的GeoJSON渲染
- 谷歌地图不显示折线
- 将标记添加到现有的谷歌地图(不刷新谷歌地图)
- 谷歌地图不显示给定语言Lat的地图
- 谷歌地图不会出现,但搜索框会出现
- 谷歌地图:不需要调整地图大小
- 我如何使谷歌地图不更改标记的自定义图标大小
- 谷歌地图不通过API显示
- 谷歌地图不居中和缩放标记
- 为什么打开页面时谷歌地图不显示
- 为什么谷歌地图不会显示在我的网站上,我一直在使用开发者网站
- 谷歌转化不起作用 - 脚本无法加载
- Javascript谷歌地图不起作用
- 带有集群的谷歌地图不起作用
- 动态加载的谷歌分析不起作用
- (懒惰)重构后加载谷歌地图不起作用
- 谷歌分析不起作用