在浏览器中调整Google地图(V3)大小(响应) [英] Center Google Maps (V3) on browser resize (responsive)

查看:113
本文介绍了在浏览器中调整Google地图(V3)大小(响应)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的网页中有一个Google地图(V3),页面宽度为100%,中间有一个标记。当我调整浏览器窗口的宽度时,我希望地图保持居中(响应)。现在地图只停留在页面的左侧并变小。

I have a Google Maps (V3) in my page at 100% page width with one marker in the middle. When I resize my browser window's width I would like the map to stay centered (responsive). Now the map just stays at the left side of the page and gets smaller.

更新
完全按照duncan的描述工作。这是最终的代码:

UPDATE Got it to work exactly as described thanks to duncan. This is the final code:

var center;
function calculateCenter() {
  center = map.getCenter();
}
google.maps.event.addDomListener(map, 'idle', function() {
  calculateCenter();
});
google.maps.event.addDomListener(window, 'resize', function() {
  map.setCenter(center);
});


推荐答案

您需要有一个事件侦听器调整大小。这对我有用(把它放在你的初始化函数中):

You need to have an event listener for when the window resizes. This worked for me (put it in your initialize function):

google.maps.event.addDomListener(window, 'resize', function() {
    map.setCenter(center);
});

这篇关于在浏览器中调整Google地图(V3)大小(响应)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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