简体   繁体   English

带有 header div 的 GoogleMap 超出了 window 大小

[英]GoogleMap with header div extends beyond window size

Note: I've seen some similar questions and tried their answers (CSS position: absolute), but they are all ~7 years old and did not solve my problem.注意:我已经看到了一些类似的问题并尝试了他们的答案(CSS position:绝对),但它们都已经 7 岁左右,并没有解决我的问题。

I am making a simple React app with an interactive Google Map with a header that displays information from the map.我正在使用交互式 Google Map 和 header 制作一个简单的 React 应用程序,该应用程序显示来自 map 的信息。 However, the map always expands a bit below the page, causing the page to scroll up/down by the same amount as the header size.但是,map 总是在页面下方扩展一点,导致页面向上/向下滚动的量与 header 大小相同。

This is a demo , which does what is described above, along with relevant code snippets: 这是一个演示,它执行上述操作,以及相关的代码片段:

HTML (served via React component, as seen in demo) HTML(通过 React 组件提供,如演示中所示)

<div>
  <div className="row-header">
  </div>

  <div className="mapContainer">
    <GoogleMap id="map" mapContainerStyle={mapContainerStyle} zoom={4} />
  </div>
</div>

CSS CSS

.row-header {
  min-height: 305px;
  display: flex;
}

.col-left {
  width: 33%;
}

.col-right {
  width: 66%;
}

.mapContainer {
  max-width: 100%;
}

After some research, I found that I should try CSS absolute positioning , however this has not helped.经过一番研究,我发现我应该尝试CSS 绝对定位,但这并没有帮助。 This is a demo of that new version: 这是该新版本的演示

HTML HTML

<div>
  <div className="row-header">
  </div>

  <div className="mapContainer">
    <GoogleMap id="map" mapContainerStyle={mapContainerStyle} zoom={4} />
  </div>
</div>

CSS CSS

.row-header {
  min-height: 305px;
  display: flex;
  position: absolute;
  top: 0px;
  left: 0px;
  right: 0px;
  height: 305px;
}

.mapContainer {
  position: absolute;
  top: 305px;
  left: 0px;
  right: 0px;
  bottom: 0px;
}

.Container {
  max-width: 100%;
  max-height: 100%;
  width: 100%;
  height: 100%;
}

As I was writing this question discovered the answer.在我写这个问题的时候发现了答案。 Figured I might as well post and share.想我不妨张贴和分享。

The CSS Absolute Position fix DOES WORK, however in this particular case the fix is not done in the CSS, but in the Javascript. The CSS Absolute Position fix DOES WORK, however in this particular case the fix is not done in the CSS, but in the Javascript. That is because @react-google-maps/api , uses mapContainerStyle={mapContainerStyle} , which seems to be a required field (although the docs don't make that clear).那是因为@react-google-maps/api使用mapContainerStyle={mapContainerStyle} ,这似乎是一个必填字段(尽管文档没有明确说明)。

After modying mapContainerStyle from从修改 mapContainerStyle 后

const mapContainerStyle = {
  height: "100vh",
  width: "100vw"
};

to

const mapContainerStyle = {
  position: "absolute",
  top: "305px",
  left: "0px",
  right: "0px",
  bottom: "0px"
};

The page displays as intended!页面按预期显示! See Demo 看演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM