I am trying to change the color of my GeoJSON
-Layer on every click in my Ionic React App, but I only managed to change it once for the first click... My idea was to change the color between blue and red everytime a feature is clicked. I had the idea to check for the color in the options
of the GeoJSON
-Layer, but as written, it only changes the color once for the first click and afterwards nothing happens on any other click.
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