在容器更改宽度后接收Google地图 [英] Recenter a Google map after container changed width

查看:116
本文介绍了在容器更改宽度后接收Google地图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Javascript API V3设置了谷歌地图。它以动态宽度显示在div中,当点击标记时内容窗格会滑动。



一切正常,但有一点:当窗格向上滑动时,地图的宽度会发生变化,所以中心向右移动(窗格是在左边)。这对于小分辨率尤其不方便,特别是对于小分辨率,在窗格打开后甚至无法看到中心。



我尝试了'调整大小'触发器,但它似乎不工作...任何想法?



非常感谢!

解决方案

通过它调用 resize 自我不会实现你所需要的东西。

你需要做的是首先(在调整大小之前)获取当前的地图中心

  var currCenter = map.getCenter(); 

然后您需要在div的大小调整后执行以下操作。

  google.maps.event.trigger(map,'resize'); 
map.setCenter(currCenter);

应该怎么办?

I have a google map set with the Javascript API V3. It's displayed in a div with a dynamic width, as a content pane slides up when we click on a marker.

Everything works fine, but one thing: When the pane slides up, the width of the map is changed, and so the center is displaced to the right (the pane is on the left). It is really inconvienient especially for small resolutions, where you can't even see the center after the pane is open.

I've tried the 'resize' trigger, but it doesn't seem to work... Any ideas ?

Thanks a lot !

解决方案

Calling resize by it self will not acheive what you need.

What you need to do is first (before a resize occurs) get the current center of the map

var currCenter = map.getCenter();

Then you need to do something like the following, after your div is resized.

google.maps.event.trigger(map, 'resize');
map.setCenter(currCenter);

Should do the trick

这篇关于在容器更改宽度后接收Google地图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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