4

I am using the simple example from leaflet-react in a barebones create-react-app app.

Problem: The map tiles do render, but there is always 1-2 rows of map tiles that do not render (grayed out). Different rows will start disappearing when the map is moved around.

enter image description here

However if i were to resize the browser window any amount at all, the map renders properly!

enter image description here

What is the problem, and how can we solve it?

Using react-leaflet v2.2.1, leaflet 1.4.0. Same problem on Chrome browser and Brave browser.

Maps.js

import React, { Component } from 'react';
import { Map, TileLayer, Marker, Popup } from 'react-leaflet';

class Maps extends Component {
    constructor() {
        super();
        this.state = {
            lat: 51.505,
            lng: -0.09,
            zoom: 13
        }
    }

    render() {
        const position = [this.state.lat, this.state.lng];

        return (
            <div>
                <Map center={position} zoom={this.state.zoom}>
                    <TileLayer
                        attribution='&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
                        url='http://{s}.tile.osm.org/{z}/{x}/{y}.png'
                    />
                    <Marker position={position}>
                        <Popup>
                            <span>A pretty CSS3 popup. <br/> Easily customizable.</span>
                        </Popup>
                    </Marker>
                </Map>

            </div>

        )
    }
}

export default Maps;

index.js

import React from 'react';
import ReactDOM from 'react-dom';
import { BrowserRouter as Router, Route, Switch } from 'react-router-dom';

import './index.css';
import Maps from './containers/Maps/Maps.js';

ReactDOM.render(
    <Router>
        <div>
            <Switch>
                <Route path="/" exact component={Maps} />
            </Switch>
        </div>
    </Router>
    , document.getElementById('root'));

index.css

.leaflet-container {
  height: 800px;
  width: 100%;
}

index.html

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <link rel="shortcut icon" href="%PUBLIC_URL%/favicon.ico" />
    <meta
      name="viewport"
      content="width=device-width, initial-scale=1, shrink-to-fit=no"
    />
    <meta name="theme-color" content="#000000" />
    <link rel="manifest" href="%PUBLIC_URL%/manifest.json" />
    <title>React App</title>
    <link rel="stylesheet" href="https://unpkg.com/leaflet@1.4.0/dist/leaflet.css"
  integrity="sha512-puBpdR0798OZvTTbP4A8Ix/l+A4dHDD0DGqYW6RQ+9jxkRFclaxxQb/SJAWZfWAkuyeQUytO7+7N4QKrDh+drA=="
  crossorigin=""/>
  </head>
  <body>
    <noscript>You need to enable JavaScript to run this app.</noscript>
    <div id="root"></div>
  </body>
</html>
kboul
  • 13,836
  • 5
  • 42
  • 53
Nyxynyx
  • 61,411
  • 155
  • 482
  • 830

2 Answers2

7

You just need to pass a height prop to your Map component instance. I believe you do not need this piece of code after this:

.leaflet-container {
  height: 800px;
  width: 100%;
}

therefore it should be

<Map 
    style={{ height: "100vh" }} 
    center={position} 
    zoom={this.state.zoom}>
      <TileLayer
          attribution='&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
          url='http://{s}.tile.osm.org/{z}/{x}/{y}.png' />
      <Marker position={position}>
            <Popup>
                 <span>A pretty CSS3 popup. <br/> Easily customizable.</span>
            </Popup>
      </Marker>
</Map>

Demo

kboul
  • 13,836
  • 5
  • 42
  • 53
  • 5
    Thanks. Made my day. Why do that react-leaflet nerds not getting a simple sample nailed? Don't get it :/ – decades Feb 22 '20 at 12:52
  • 1
    If anyone is still having issuses make sure to have `` in your index.html – Giacomo A. Feb 24 '21 at 17:52
  • 1
    Ya, why did the authors of this library NOT put this into the sample code that's in their own setup guide?? – rm.rf.etc Nov 03 '21 at 03:04
  • As of today I'm using instead of and this bug seems to persist? at least the style attribute within MapContainer does nothing anymore for me... Anyone with the same problem? – ch1ll Feb 22 '22 at 19:45
  • You have an error probably in another part of your code. Make sure you have imported lealfet.css – kboul Feb 22 '22 at 21:13
  • 1
    I set the height of the MapContainer to 90% but I never set an absolute height on any of the divs it was in. As soon as I set the height in absolute pixels, it worked. So for now it's okay, but later in the project I need to deal with some css :D Thank you for the hint! – ch1ll Mar 02 '22 at 09:28
3

As @Giacomo A. mentioned in a comment:

make sure to have:

<link rel="stylesheet" href="https://unpkg.com/leaflet@1.4.0/dist/leaflet.css"  integrity="sha512-puBpdR0798OZvTTbP4A8Ix/l+A4dHDD0DGqYW6RQ+9jxkRFclaxxQb/SJAWZfWAkuyeQUytO7+7N4QKrDh+drA=="  crossorigin=""/> 

in your index.html.

Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
bvn13
  • 154
  • 11