【问题标题】:React leaflet center attribute does not change when the center state changes当中心状态改变时,React 传单中心属性不会改变
【发布时间】:2021-10-22 15:04:05
【问题描述】:

App.js

import { useState } from 'react';

const App = () => {
  // This state is used to the center attribute of MapContainer component
  const [mapCenter, setMapCenter] = useState([34.80746, -40.4796]);
  // This state is used to the zoom attribute of MapContainer component
  const [mapZoom, setMapZoom] = useState(3);

  const onClickHandler = () => {
    setMapCenter([20, 100]);
    setMapZoom(5);
  };

  return (
    <>
      <button onClick={onClickHandler}>Change map's center location</button>
      <Map center={mapCenter} zoom={mapZoom} />
    </>
  );
};

Map.js

import React from 'react';
import './Map.css';
import { MapContainer, TileLayer } from 'react-leaflet';

function Map({ center, zoom }) {
  return (
    <div className="map">
      <MapContainer center={center} zoom={zoom}>
        <TileLayer
          url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
          attribution='&copy; <a 
                href="http://osm.org/copyright">OpenStreetMap</a> contributors'
        />
      </MapContainer>
    </div>
  );
}

export default Map;

Map.css

.map {
  height: 500px;
  background-color: white;
  padding: 1rem;
  border-radius: 20px;
  margin-top: 16px;
  box-shadow: 0 0 8px -4px rgba(0, 0, 0, 0.5);
}

.map .leaflet-container {
  height: 100%;
}

当我单击按钮时,mapCenter 状态明显更改为 [20, 100],mapZoom 也更改为 5。但是在 Map Component Map 中没有显示新的中心。但我不知道为什么。我已经检查了状态的变化。 地图从不响应。有谁知道???请回答一个解决方法。

【问题讨论】:

    标签: reactjs leaflet


    【解决方案1】:
    const Map = ({ center, zoom }) => {
      const [map, setmap] = useState(null);
      if (map) {
        map.flyTo(center);
      }
      return (
        <div className="map">
          <LeafletMap
            center={center}
            zoom={zoom}
            whenCreated={setmap}
            scrollWheelZoom={false}
          >
            <TileLayer
              url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
              attribution='&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
            ></TileLayer>
          </LeafletMap>
        </div>
      );
    };
    

    【讨论】:

    • 嗨,欢迎来到 Stack Overflow!在回答问题时,请附上一些评论,以便提问者和其他读者了解您的解决方案。
    【解决方案2】:

    除了children,MapContainer 属性是不可变的:第一次设置后更改它们不会对 Map 实例或其容器产生影响。 由 MapContainer 元素创建的 Leaflet Map 实例可以由子组件使用提供的钩子之一或 MapConsumer 组件访问。
    https://react-leaflet.js.org/docs/api-map

    ChangeView.js

    function ChangeView({ center, zoom }) {
      const map = useMap();
      map.setView(center, zoom);
      return null;
    }
    

    Map.js

    function Map({ center, zoom }) {
      return (
        <div className="map">
          <MapContainer center={center} zoom={zoom} scrollWheelZoom={false}>
            <ChangeView center={center} zoom={zoom} /> 
            <TileLayer
              url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
              attribution='&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
            />
          </MapContainer>
        </div>
      );
    }
    

    react-leaflet 文档在它的useMapEvent example 中使用了setCenter,但是没有定义这个函数。然而在传单文档 Methods for modifying map state 中有一个函数 setView(center, zoom, options?)。

    【讨论】:

    • 这很有帮助。谢谢您的回答。我终于解决了。
    • @Banny 你可以只渲染其他东西,直到你有中心坐标。例如。 (如加载动画)
    猜你喜欢
    • 2011-10-26
    • 2019-05-15
    • 2020-04-22
    • 2019-12-30
    • 2020-05-03
    • 2020-08-17
    • 2020-01-20
    • 1970-01-01
    相关资源
    最近更新 更多