reactjs 将div添加到Map瓣叶React

icnyk63a  于 2023-02-18  发布在  React
关注(0)|答案(1)|浏览(110)

早安
我想在宣传单卡片上添加一个块div,并在div Reactjs中添加文本。
但它未显示在Map上。它显示在Map下方,我尝试使用z-index,但未成功
有人有解决办法吗?

<div >

         <MapContainer center={center} zoom={zoom} style={{ height: "90vh", opacity: 'O,33' }}>
            <TileLayer attribution='&amp;copy <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
               url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png" />
            <Markers marker={mark} />
            <div>Test</div>
         </MapContainer >

      </div>);```
6ojccjat

6ojccjat1#

您可以使用z-index,但是您还必须将position设置为默认静态值以外的值,例如position: absolute
我建议查看内置的leaflet-control类。我使用一个自定义控制器在Map上添加东西:

import L from "leaflet";
import React, { useEffect, useRef } from "react";

const ControlClasses = {
  bottomleft: "leaflet-bottom leaflet-left",
  bottomright: "leaflet-bottom leaflet-right",
  topleft: "leaflet-top leaflet-left",
  topright: "leaflet-top leaflet-right",
};

type ControlPosition = keyof typeof ControlClasses;
export interface LeafLetControlProps {
  position?: ControlPosition;
  children?: React.ReactNode;
  offset?: [number, number];
}

const LeafletControl: React.FC<LeafLetControlProps> = ({
  position,
  children,
  offset = [0, 0],
}) => {
  const divRef = useRef(null);

  useEffect(() => {
    if (divRef.current) {
      L.DomEvent.disableClickPropagation(divRef.current);
      L.DomEvent.disableScrollPropagation(divRef.current);
    }
  });

  return (
    // @ts-ignore
    <div
      style={{
        marginLeft: offset[0],
        marginTop: offset[1],
      }}
      ref={divRef}
      className={position && ControlClasses[position]}
    >
      <div className={"leaflet-control"}>{children}</div>
    </div>
  );
};

export default LeafletControl;

相关问题