首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >React Leaflet Legend with MapContainer

React Leaflet Legend with MapContainer
EN

Stack Overflow用户
提问于 2021-02-19 18:23:35
回答 1查看 422关注 0票数 1

早上好,我知道有一个副本,但建议的解决方案不起作用,我想把一个简单的图例与MapContainer,你能帮我一个忙,使与反应传单3.x的图例组件?

版本React-Leaflet 3.1.0这是我的地图

代码语言:javascript
复制
<MapContainer
    style={{ height: 480, width: "97.5%" }}
    zoom={2}
    center={[30.09, 51.505]}
    scrollWheelZoom={false}
    fadeAnimation={true}
    markerZoomAnimation={true}
  >
    <TileLayer
      url={urlLayer}
      test="http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
    />
    <Legend />
    <CircleMarker
      center={[51.505, -0.09]}
      color={"#000000"}
      fillColor={"#FDD876"}
      stroke={true}
      fillOpacity={true}
      weight={1}
    >
      <Popup>
        A pretty CSS3 popup. <br /> Easily customizable.
      </Popup>
    </CircleMarker>

   </MapContainer>
EN

回答 1

Stack Overflow用户

发布于 2021-02-19 18:48:34

我插入带有地图钩子的whenCreated

代码语言:javascript
复制
  const [map, setMap] = useState(null);
<MapContainer
    style={{ height: 480, width: "97.5%" }}
    zoom={2}
    center={[30.09, 51.505]}
    scrollWheelZoom={false}
    fadeAnimation={true}
    markerZoomAnimation={true}
    whenCreated={setMap}
  >

图例

代码语言:javascript
复制
import L from "leaflet";
import { useEffect } from "react";
import './LegendComponent.css'
function Legend({ map }) {
  console.log(map);
  useEffect(() => {
    if (map) {
      const legend = L.control({ position: "bottomright" });

      legend.onAdd = () => {
        const div = L.DomUtil.create("div", "info legend");
        div.innerHTML =
          "<h4>This is the legend</h4>" +
          "<b>Lorem ipsum dolor sit amet consectetur adipiscing</b>";
        return div;
      };

      legend.addTo(map);
    }
  }, [map]); //here add map
  return null;
}

export default Legend;
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66275764

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档