react-leaflet map not correctly displayed
Solution 1:
Looks like you haven't loaded in the Leaflet stylesheet.
From the react-leaflet
GitHub guide:
If you are not familiar with Leaflet, make sure you read its quick start guide before using this library. You will notably need to add its CSS to your page to render the map properly, and set the height of the container.
http://leafletjs.com/examples/quick-start/
Here is what you'll need:
<link rel="stylesheet" href="https://unpkg.com/[email protected]/dist/leaflet.css" />
Update
Note @ThomasThiebaud indicates you may also have to set up the height of .leaflet-container
--
Ange Loron also gave a correct, optional, JS module import (vs cdn or style link)
import 'leaflet/dist/leaflet.css';
For what its worth, the documentation page is poorly designed... and the maintainer continuously deals with this issue in GitHub, but for some reason, the issue is the *fault of the users who continuously don't do the required setup. /s
Solution 2:
I am also new to using this library and didn't find the documentation clear enough. But here are few things I find necessary in order for this to work.
1. react-leaflet package
2. Leaflet package:
Either, install it using npm
npm install leaflet
andimport 'leaflet/dist/leaflet.css';
in the file where you use Map
from react-leaf
.
OR
Include these two lines in the index.html
:
<link rel="stylesheet" href="https://unpkg.com/[email protected]/dist/leaflet.css"
integrity="sha512-xwE/Az9zrjBIphAcBb3F6JVqxf46+CDLwfLMHloNu6KEQCAWi6HcDUbeOfBIptF7tcCzusKFjFw2yuvEpDL9wQ=="
crossorigin=""/>
<!-- Make sure you put this AFTER Leaflet's CSS -->
<script src="https://unpkg.com/[email protected]/dist/leaflet.js"
integrity="sha512-gZwIG9x3wUXg2hdXF6+rVkLF/0Vi9U8D2Ntg4Ga5I5BZpVkVxlJWbSQtXPSiUTtC0TjtGOmxa1AJPuV0CPthew=="
crossorigin=""></script>
3. Add this to a App.css or index.css and import the file: (And its a must)
.leaflet-container {
width: 100wh;
height: 100vh;
}
// OR add style directly to the map container
<Map
center={position}
zoom={1}
style={{ height: '100vh', width: '100wh' }}
>
<TileLayer .... />
</Map>
Solution 3:
Just in case someone runs into the same issue, I solved it by simply adding this:
import 'leaflet/dist/leaflet.css';