I'm trying to build a map with a selectable and dynamic layer URL with the code below.
When I try to change the layer URL by the state it shows the error in the screenshot attached.
I have stored all the tiles in the tiles array variable and I'm trying to change the URL by changing the index of the tiles. The first time I run the project it works fine, but when I click the button and try to change the index of the tile to render another map tile it shows the error in the screenshot. I also should mention that there are no errors shown in the console tab of developer tools chrome. Is there a way to solve this problem? Any idea would be appreciated :)
Screenshot of error:
const LeafletContainer = () => {
const [baseViewCoords, setBaseViewCoords] = useState([37.715, 44.8611]);
const [map, setMap] = useState();
const merged_20181223 = L.tileLayer(
"dymmyurl",
{ tms: true, opacity: 1, attribution: "", minZoom: 1, maxZoom: 16 }
);
const merged_20180924 = L.tileLayer(
"dymmyurl",
{ tms: true, opacity: 1, attribution: "", minZoom: 1, maxZoom: 16 }
);
const merged_20190323 = L.tileLayer(
"dymmyurl",
{ tms: true, opacity: 1, attribution: "", minZoom: 1, maxZoom: 16 }
);
const merged_20190626 = L.tileLayer(
"dymmyurl",
{ tms: true, opacity: 1, attribution: "", minZoom: 1, maxZoom: 16 }
);
const merged_20190919 = L.tileLayer(
"dymmyurl",
{ tms: true, opacity: 1, attribution: "", minZoom: 1, maxZoom: 16 }
);
const merged_20191218 = L.tileLayer(
"dymmyurl",
{ tms: true, opacity: 1, attribution: "", minZoom: 1, maxZoom: 16 }
);
const merged_20200625 = L.tileLayer(
"dymmyurl",
{ tms: true, opacity: 1, attribution: "", minZoom: 1, maxZoom: 16 }
);
const merged_20200918 = L.tileLayer(
"dymmyurl",
{ tms: true, opacity: 1, attribution: "", minZoom: 1, maxZoom: 16 }
);
const tiles = [
merged_20190323,
merged_20191218,
merged_20181223,
merged_20180924,
merged_20190626,
merged_20190919,
merged_20200625,
merged_20200918,
];
const [leftTileIndex, setLeftTileIndex] = useState(0);
const [rightTileIndex, setTRightTileIndex] = useState(7);
const changeTileHandler = () => {
setLeftTileIndex((prev) => {
if (leftTileIndex === tiles.length - 1) {
return 0;
}
return prev + 1;
});
};
useEffect(() => {
if (map) {
L.control
.splitMap(
L.tileLayer(tiles[leftTileIndex]._url, {
tms: true,
opacity: 1,
attribution: "",
minZoom: 1,
maxZoom: 16,
}).addTo(map),
L.tileLayer(tiles[rightTileIndex]._url, {
tms: true,
opacity: 1,
attribution: "",
minZoom: 1,
maxZoom: 16,
})
.addTo(map)
.addTo(map)
)
.addTo(map);
}
}, [map, leftTileIndex, rightTileIndex]);
useEffect(() => {
if (map) {
map.setView(baseViewCoords);
}
}, [map, baseViewCoords]);
return (
<div style={{ position: "relative" }}>
<SearchArea
{...{
changeTileHandler,
}}
/>
<Options />
<Coordinate {...{ baseViewCoords }} />
<div id="map">
<MapContainer
style={{ height: "100%" }}
center={baseViewCoords}
zoom={9}
scrollWheelZoom={true}
whenCreated={(map) => {
setMap(map);
}}
>
<TileLayer
tms={true}
minZoom={1}
maxZoom={16}
opacity={1}
attribution=""
url={tiles[leftTileIndex]._url}
/>
<TileLayer
minZoom={1}
maxZoom={16}
opacity={1}
tms={true}
attribution=""
url={tiles[rightTileIndex]._url}
/>
</MapContainer>
</div>
</div>
);
};
export default LeafletContainer;