【问题标题】:Change the center of google maps on browser size在浏览器大小上更改谷歌地图的中心
【发布时间】:2013-01-30 22:01:06
【问题描述】:

我是一个 js 菜鸟,我想输入一行代码,一旦我的浏览器小于 675 像素,就会改变我的谷歌地图的中心点。我正在使用 v3 的 Google 地图 API。

到目前为止,我的这段代码似乎不起作用。

$(function() {  
  if($('#pgWidth').width() <= 675){
  map.setCenter(new google.maps.LatLng(51.50000 , -0.30000));
  } else {
  map.setCenter(new google.maps.LatLng(51.50000, -0.30200));
  };
});

【问题讨论】:

  • 在什么情况下不起作用?你的 IF-ELSE 语句也做同样的事情
  • 我更改了帖子的代码,所以现在修改了 else 语句!更改浏览器大小时,中心点不会改变

标签: javascript google-maps google-maps-api-3


【解决方案1】:

您需要处理调整大小事件,而不是只在文档就绪时运行代码:

$(window).resize(function() {
  if($(window).width() <= 675) {
    map.setCenter(new google.maps.LatLng(51.50000 , -0.30000));
  } else {
    map.setCenter(new google.maps.LatLng(51.50000, -0.30200));
  };
});

【讨论】:

  • 感谢您的帮助,但这似乎也不起作用,我可以提供任何其他代码来提供帮助吗?抱歉,我对这一切都不熟悉 :(
  • 不是检查#pgWidth的宽度,你不应该检查窗口本身的宽度吗?
  • 非常感谢你们的帮助! :)
猜你喜欢
  • 1970-01-01
  • 2012-07-20
  • 1970-01-01
  • 1970-01-01
  • 2017-07-06
  • 1970-01-01
  • 2013-02-25
  • 2012-08-29
  • 1970-01-01
相关资源
最近更新 更多