I want to add more than one tilelayers to mapcontainer. I am not able to accomplish that. I have one tilelayer like this
const magnifiedTiles = L.tileLayer("https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png")
and another one here:
export default function App(): JSX.Element {
return (
<>
<MapContainer center={center} zoom={13} scrollWheelZoom={true} style={{height: 'calc(100% - 30px)'}}>
<TileLayer
attribution='© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
url='https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png'
/>
</>)
}
I can either get a map reference from map container and use like magnifiedTiles.addTo(<map>)
but I dont know how to get the map reference. Other option is addLayer()
. However, I am not able to access it with from L.Tilelayer.
I know it leaflet it was as simple as getting map reference and using map.addLayer()
.
You can easily get the map reference via a ref in react-leaflet v.4.x
and then use a useEffect to add any layer you want.
Edit after you added the typescript tag: Same as before only thing that changes you should install @types/react-leaflet and declare the map ref type.
Source for streets layer that includes token
Demo