Leaflet ReactJS Map 不完全显示图块

Leaflet ReactJS Map does not show tile completely

我正在尝试创建一个位于模式内部的地图。但是地图只显示了一部分。我在创建节点后尝试 invalidatingSize() 但它似乎不起作用。

import React from 'react';
import ReactDOM from 'react-dom'
import L from 'leaflet';

class Mapa extends React.Component {
    constructor(props){
        super(props);
        this.state = {
        };
    }

    createMap(element){
        var map = L.map(element);
        L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png', {
            attribution: '&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
        }).addTo(map);
        return map;
    }

    setupMap(){
        this.map.setView([this.props.lat, this.props.lon], this.props.zoom);
        this.map.invalidateSize();
    }

    componentDidMount(){
        let self = this;
        if (this.props.createMap) {
            this.map = this.props.createMap(ReactDOM.findDOMNode(self));
        } else {
            this.map = this.createMap(ReactDOM.findDOMNode(self));
        }

        this.setupMap();
    }

    render(){
        /*Returns div with id map*/
    }
}

我使用 JSS:

import jss from 'jss';
import jssDefault from 'jss-preset-default';

import 'leaflet/dist/leaflet.css';

jss.setup(jssDefault());

jss.createStyleSheet({
  '@global': {
    '.leaflet-container': {
      height: '100%',
    },
  },
}).attach();

Leaflet 使用全局 CSS 类。所以你可以使用你的全局样式 css-loader:

styles.css:

:global {
  .leaflet-container {
    height: 100%;
  }
}

component.jsx:

import './styles.css';

终于修好了。代码是正确的,但我没有正确添加传单依赖项。一旦我修好了,一切都完美无缺。事实上 this.map.invalidateSize();没必要。

    import React from 'react';
    import ReactDOM from 'react-dom'
    import L from 'leaflet';

    class Mapa extends React.Component {
        constructor(props){
            super(props);
            this.state = {
            };
        }

        createMap(element){
            var map = L.map(element);
            L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png', {
                attribution: '© OpenStreetMap contributors'
            }).addTo(map);
            return map;
        }

        setupMap(){
            this.map.setView([this.props.lat, this.props.lon], this.props.zoom);
        }

        componentDidMount(){
            let self = this;
            if (this.props.createMap) {
                this.map = this.props.createMap(ReactDOM.findDOMNode(self));
            } else {
                this.map = this.createMap(ReactDOM.findDOMNode(self));
            }

            this.setupMap();
        }

        render(){
            /*Returns div with id map*/
        }
    }