【发布时间】:2018-04-20 09:32:56
【问题描述】:
我在我的网站上使用 Google Maps API。我想要一个可以展开和折叠地图的按钮。问题是,如果我加载高度小的地图,当我增加高度(通过 JS)时,地图不会填满整个区域,而是有一个灰色空间(见下图)。有趣的是,虽然 div 从 200px 开始(在 CSS 中设置),但它下方的地图(展开后)更大。它有一些价值,我不知道它是从哪里得到的。
我尝试调用“google.maps.event.trigger(map, 'resize');”但这无济于事。
这是按下按钮时运行的 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。
【问题讨论】:
-
地图未定义。查看您可能想要的链接this.map
-
你是对的。我无法相信解决方案是如此明显。谢谢。现在它起作用了。但是,它不适用于动画(过渡)。当我从 css 中删除这 5 行时,它工作正常。但我也想要那个动画。
标签: javascript css google-maps-api-3