1
votes

Hi I'm trying to create a map that is inside of a modal. But the map is only showed partially. I've tried invalidatingSIze() after node is created but it doesn't seem to work. Thanks!


    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);
            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*/
        }
    }

2
What does your css file look like? Sometimes you need to set the heigh and width of the map div to 100%.Evan Siroky
try calling this.map.invalidateSize(); inside a setIntervalGaurav Mukherjee

2 Answers

1
votes

I did this using the 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 uses global CSS classes. So you can just use your global styles using css-loader:

styles.css:

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

component.jsx:

import './styles.css';
1
votes

Finally I fixed. The code is correct but I didn't add leaflet dependencies correctly. Once I fixed that all worked perfectly. In fact this.map.invalidateSize(); was no necessary.

    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*/
        }
    }