【问题标题】:Leaflet: Change color onClick传单:点击更改颜色
【发布时间】:2021-11-05 21:02:17
【问题描述】:

我试图在我的 Ionic React 应用程序中的每次单击时更改我的GeoJSON-Layer 的颜色,但我只设法在第一次单击时更改它一次...我的想法是在蓝色之间更改颜色每次单击功能时都会显示红色。我的想法是检查GeoJSON-Layer 的options 中的颜色,但正如所写,它只会在第一次单击时更改一次颜色,然后在任何其他单击时都不会发生任何事情。

function LeafletMap() {
  const [map, setMap] = useState(null)

  const onEachClick = (info, layer) => {
    const part = info.properties.Objekt_ID
    const id = info.properties.FID_

    layer.bindPopup("Object ID: <b>" + part + "</b><br>FID: <b>" + id + "</b>")

    if(layer.options.color != "blue") {
      layer.on({
        click: (event) => {
          event.target.setStyle({
            color: "blue",
          });
        }
      }) 
    } else {
      layer.on({
        click: (event) => {
          event.target.setStyle({
            color: "red",
          });
        }
      }) 
    }
  }

  const displayMap = useMemo(
    () => (
      <MapContainer
        center={center}
        zoom={zoom}
        scrollWheelZoom={false}
        whenCreated={setMap}>
        <LayersControl position="topright">
          <LayersControl.BaseLayer checked name="OpenStreetMap - Map">
            <TileLayer
              attribution='&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
              url="https://{s}.tile.openstreetmap.de/tiles/osmde/{z}/{x}/{y}.png"
            />
          </LayersControl.BaseLayer>
          <LayersControl.BaseLayer name="Esri - Satelite">
            <TileLayer
              attribution='Tiles &copy; Esri &mdash; Source: Esri, i-cubed, USDA, USGS, AEX, GeoEye, Getmapping, Aerogrid, IGN, IGP, UPR-EGP, and the GIS User Community'
              url="https://server.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer/tile/{z}/{y}/{x}"
            />
          </LayersControl.BaseLayer>
        </LayersControl>

        <GeoJSON data={PL.features} onEachFeature={onEachClick} color="blue"/>
        <GeoJSON data={WWD.features} onEachFeature={onEachClick} color="blue"/>
      </MapContainer>
    ),
    [],
  )

  window.dispatchEvent(new Event('resize'));

  return (
    <div>
      {displayMap}
    </div>
  )

}
export default LeafletMap

【问题讨论】:

  • 如果您可以在codesandbox或stackblitz上分享更多代码,它会更容易帮助您...
  • 这是整个代码,剩下的只是应用界面的渲染。我试图让它在 JSFiddle 上运行,但不幸的是,到目前为止我还没有设法让我的 React 应用程序在 JSFiddle 上运行

标签: javascript reactjs leaflet geojson react-leaflet


【解决方案1】:

有一种非常简单的方法可以实现所需的行为。我可以举一个使用 geojson 的示例,然后您可以根据需要对其进行调整。

您需要在每次点击图层后重置 geojson 样式。您可以通过使用 react ref 和传单的 resetStyle 方法获取 geojson 参考来实现这一点。而关于样式的变化,您只需在每次点击后设置颜色。那里不需要 if 语句。

const geoJsonRef = useRef();

  const onEachClick = (feature, layer) => {
    const name = feature.properties.name;
    const density = feature.properties.density;

    layer.bindPopup(
      "Name: <b>" + name + "</b><br>Density: <b>" + density + "</b>"
    );

    layer.on({ click: handleFeatureClick });
  };

  const handleFeatureClick = (e) => {
    if (!geoJsonRef.current) return;
    geoJsonRef.current.resetStyle();

    const layer = e.target;

    layer.setStyle({ color: "red" });
  };

...
<GeoJSON ref={geoJsonRef} />

这是demo,其中一些数据来自传单网站

【讨论】:

  • 感谢@kboul 的回答,帮了我很大的忙!您知道如何使用多个 对象来处理它吗?因为我不想为它们中的每一个声明一个 useRef() 和 handleFeatureClick()?
  • nvm 只是用一个 GeoJSON 元素添加了所有内容,按预期工作
猜你喜欢
  • 2021-10-08
  • 1970-01-01
  • 2018-10-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-24
相关资源
最近更新 更多