javascript - Google map API - 如何更改 map 高度?

标签 javascript css google-maps-api-3

我在我的网站上使用 Google Maps API。我想要一个可以展开和折叠 map 的按钮。问题是,如果我加载高度较小的 map ,当我增加高度(通过 JS)时, map 不会填充整个区域,而是出现一个灰色空间(见下图)。有趣的是,虽然 div 从 200px 开始(在 CSS 中设置),但它下面的 map (扩展后)更大。它有一些我不知道从哪里得到的值(value)。

我尝试调用“google.maps.event.trigger(map, 'resize');”但这没有任何作用。

折叠:enter image description here

扩展:enter image description here

这是按下按钮时运行的 JS 代码:

changeMapSize(btn) {
    if (this.size === 'expanded') {
        this.css('height', '200px');
        google.maps.event.trigger(map, 'resize');
        this.size = 'collapsed';
        btn.text('Expand map');
    }
    else {
        this.css('height', '400px');
        google.maps.event.trigger(map, 'resize');
        this.size = 'expanded';
        btn.text('Hide map');
    }
}

CSS:

/* Always set the map height explicitly to define the size of the div
   * element that contains the map. */
   #map {
    height: 200px;
    -moz-transition: height .5s;
    -ms-transition: height .5s;
    -o-transition: height .5s;
    -webkit-transition: height .5s;
    transition: height .5s;
  }
  /* Optional: Makes the sample page fill the window. */
  html, body {
    height: 100%;
    margin: 0;
    padding: 0;
  }

如果需要的话,可以看一下代码here .

最佳答案

此问题已得到解答here

特别是,google.maps.event.trigger(map, 'resize') 不再是必需的,也不再是 Maps API 的一部分。

还要检查您是否正在调整 map div 的大小,而不是调整包含 map div 的 div 的大小。

关于javascript - Google map API - 如何更改 map 高度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47167131/

相关文章:

javascript - 优化 Google map 中的 SVG 标记

javascript - 如何使 jQuery .load() 将返回的数据附加到容器内而不是重写它?

javascript - 根据 JavaScript 中的屏幕大小删除其他元素

javascript - Moment js时间戳

html - Firefox 位置固定元素 flash

javascript - 谷歌地图加载时间较长

javascript - 如何识别 Service Worker 中正确的持久数据?

javascript - 在导航栏上更改 'current' 类

javascript - 使用JS覆盖LESS css中的全局变量

javascript - Google Map v3 - getPosition 不是函数