【问题标题】:How to load a <div> on top of React google map component?如何在 React 谷歌地图组件之上加载 <div>?
【发布时间】:2021-10-09 04:27:48
【问题描述】:

我试图在这个https://www.npmjs.com/package/@react-google-maps/api 包提供的谷歌地图组件上显示一张小卡片。我将 zIndex 设置为 2,但 &lt;div&gt; 仍显示在此地图组件下方。

卡片元素不应该响应任何点击它只是在第一次加载地图组件时显示

  return (
    <div>
      <GoogleMap
        mapContainerStyle={mapContainerStyle}
        zoom={8}
        center={center}
      ></GoogleMap>
      <Tracking style={{zIndex: 2}}/>
    </div>
  );

tracking是包含信息的div。

地图容器样式为:

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

【问题讨论】:

  • 你能分享你目前拥有的代码吗?
  • 检查重新编辑我放了一些返回码。我将 zindex 设置为 2,以便它显示结束但正在下降
  • @Abidi 我已经尝试在这个例子中将我的返回 div 作为一个包装器,并将绝对位置提供给跟踪组件,但它仍然在下面

标签: css reactjs react-google-maps


【解决方案1】:

其实有很多方法可以做到。

简单地说:

  1. 您必须将position: 'relative' 添加到父组件。
  2. 您必须将position: 'absolute'zIndex: 0 添加到要显示为背景的子组件中。
  3. 您必须将position: 'absolute'zIndex: 1(或更多)添加到要显示为前景的子组件中。

如果你使用内联样式,你可以这样做:

添加top: some_numberleft: some_numberright: some_numberbottom: some_number 是可选的(取决于您想要的位置)

这是工作代码沙箱https://codesandbox.io/s/compassionate-rhodes-qbm43

App.js

import "./styles.css";
import Map from "./Map.js";
import Tracking from "./Tracking";

export default function App() {
  return (
    <div
      style={{
        position: "absolute",
        zIndex: 0,
        width: "100%", // or you can use width: '100vw'
        height: "100%" // or you can use height: '100vh'
      }}
    >
      <Map />
      <div
        style={{
          zIndex: 1,
          position: "absolute",
          top: 10,
          left: 10,
          backgroundColor: "white", // you can use any color value
          width: "10%", // or you can use width: any_number
          height: "90%" // or you can use height: any_number
        }}
      >
        <Tracking />
      </div>
    </div>
  );
}

地图.js

import React from "react";
import {
  GoogleMap,
  useLoadScript,
  Marker,
  InfoWindow
} from "@react-google-maps/api";
import Tracking from "./Tracking";
const libraries = ["places"];

const mapContainerStyle = {
  position: "relative",
  width: "100%",
  height: "100%"
};

const center = {
  lat: -33.86882,
  lng: 151.20929
};

function Map() {
  const { isLoaded, loadError } = useLoadScript({
    googleMapsApiKey: process.env.REACT_APP_GOOGLE_MAPS_API_KEY,
    libraries
  });

  if (loadError) return "Error loading maps";
  if (!isLoaded) return "Loading Maps";
  return (
    <div
      style={{
        position: "absolute",
        zIndex: 0,
        width: "100%", // or you can use width: '100vw'
        height: "100%" // or you can use height: '100vh'
      }}
    >
      <GoogleMap
        mapContainerStyle={mapContainerStyle}
        zoom={8}
        center={center}
      ></GoogleMap>
    </div>
  );
}

export default Map;

Tracking.js

import React from "react";

const Tracking = () => {
  return (
    <div>
      Lorem Ipsum is simply dummy text of the printing and typesetting industry.
    </div>
  );
};

export default Tracking;

【讨论】:

  • 如何将zIndex 与大数字(超过2)一起使用,例如zIndex: 99
  • 即使在 zIndex: 99 之后它仍然在地图下方
  • 我确实这样做了,地图完全消失了,它只显示了跟踪组件
  • 您能否为我创建一个小提琴或代码沙箱codesandbox.io/s?所以我可以直接编辑和运行它
  • 第二次编辑是在地图完全消失的情况下,第三次编辑时,div 仍在下方。
猜你喜欢
  • 2018-07-03
  • 2014-05-22
  • 2016-10-12
  • 2019-01-08
  • 1970-01-01
  • 2020-03-28
  • 1970-01-01
  • 1970-01-01
  • 2018-07-07
相关资源
最近更新 更多