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

查看:20
本文介绍了在浏览器调整大小(响应式)上居中 Google Maps (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.

更新多亏了邓肯,它才能完全按照描述的那样工作.这是最终代码:

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 Maps (V3)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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