【发布时间】: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='© <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 © Esri — 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