使谷歌地图响应调整大小

时间:2013-01-11 23:07:57

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

  

可能重复:
  Resizing google map according to browser resizing

所以通常谷歌地图使用js api看起来像这样......

enter image description here

现在,如果我要刷新页面,使用开发人员工具,然后将开发人员工具放一点,就会看起来像这样。

enter image description here

所以我想知道如何在重新调整窗口大小时重绘控件并调整地图大小。我在这里试过这段代码。但是没有去......

#map_canvas {width:100%;height:100%;position: absolute; top: 42px; left: 0; right: 0;
 bottom:0; z-index: 1; overflow: hidden; }

1 个答案:

答案 0 :(得分:2)

当window.onresize事件发生时,在#map_canvas上设置样式属性:

window.onresize = function(event) {
    var style = document.getElementById('map_canvas').style;
    style.width = '100%';
    style.height = '100%';
    style.position = 'absolute';
    style.top = '42px';
    style.left = '0';
    style.right = '0';
    style.bottom = '0';
    style.z-index = '0';
    style.overflow = 'hidden';
    google.maps.event.trigger(map, 'resize');
    map.setZoom(map.getZoom());
}

jQuery equiv考虑到顶部的42px

window.onresize = function(event) {
    var el = $("#map_canvas");
    el.css("position", "absolute");
    el.css("top", "42px");
    el.css("left", "0px");
    el.css("right", "0px");
    el.css("bottom", "0px");
    el.css("width", "100%");
    el.css("height", $("body").height() - 42);
    el.css("overflow", "hidden");
    google.maps.event.trigger(Maps.map, 'resize');
    Maps.map.setZoom(Maps.map.getZoom());
}