React-leaflet: map rendering issue

Created on 15 Dec 2015  路  21Comments  路  Source: PaulLeCam/react-leaflet

Hi Paul,

I'm trying to use leaflet with https://github.com/davezuko/react-redux-starter-kit but the map is not rendering correctly for me. I'm running node 5.2.0 and my leaflet component looks like this:

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

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

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

export default MyMap

screen shot 2015-12-15 at 15 47 40

Most helpful comment

To those having trouble getting the styles to display correctly, try adding:
<link href='https://cdnjs.cloudflare.com/ajax/libs/leaflet/1.0.3/leaflet.css' rel='stylesheet'>
to your index.html file.

If you're using webpack and have a file-loader present, you can instead load the css into a component with import 'leaflet/dist/leaflet.css'

All 21 comments

Your container div needs and absolute height and width and you need to make sure you include leaflet CSS file. Can you confirm you are doing these steps?

I use this lib in my React app and had the same issue as you before I did this.

Thanks Angus,

My styling wasn't being applied. I've managed to make it work by applying the styling inside index.html like below but I'm not sure why my styling to the react component wasn't being applied (I'm fairly new to this stuff).

<!doctype html>
<html lang="en">
<head>
  <title>React Redux Starter Kit</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/leaflet/0.7.7/leaflet.css">
  <style>
    h1, h2, p {
      text-align: center;
    }
    .leaflet-container {
      height: 400px;
      width: 80%;
      margin: 0 auto;
    }
  </style>
</head>
<body>
  <div id="root"></div>
</body>
</html>

Sorry for posting on this, but didn't want to open a new topic.

I'm also having this weird rendering issue (I've a bunch of different things: different tiles, setting height, removing everything but the map).
Rendering Issue Screenshot

Demo is viewable here.
Code is viewable here.
I've seen the size rendering issue before, but this doesn't seem to be it because I've tried invalidating size as well as setting size directly on #map1.

It was working before too which is the weirdest thing, also the points on the map are in the correct places.
It broke after I added the playing video and won't work anymore even after removing it and it's npm package.

Turns out my issue was due to having styling on generic img tags.

To whom it may concern: adding the .leaflet-container css is super important. Spent several hours trying to make it work without that - I failed.

Can we add this to the "Getting Started" Documentation? I almost gave up on Leaflet until I found this thread.

can we pass through a style={{ height: 200 }} to the <Map> component?

To those having trouble getting the styles to display correctly, try adding:
<link href='https://cdnjs.cloudflare.com/ajax/libs/leaflet/1.0.3/leaflet.css' rel='stylesheet'>
to your index.html file.

If you're using webpack and have a file-loader present, you can instead load the css into a component with import 'leaflet/dist/leaflet.css'

@duhaime
you save my day! with:
import 'leaflet/dist/leaflet.css'

@angusmack - thank you very much. this was exactly what I needed

Hi
I am having the same problem and can not be fixed while adding style to it??
does any one know another way of how i can fix that ??

I am using GoogleMutant to load Google Map using React-Leaflet. I have set the height and width and have even added the required styles, however, the map only gets rendered when I resize the browser window. Please help!

HI!
sorry i dont have any idea how to fix it in React but i hade fix my issue i use polymer 2 by adding style tag inside dem model :)

Thank you for pointing to the CSS file! Was going crazy trying to figure out why the tiles were hectic.

Now have a working react Map component. Heres the code for anyone struggling.
import React from "react";
import L from "leaflet";
import Leaflet from 'leaflet/dist/leaflet.css'

const style = {
width: "100%",
height: "300px"
};

class Map extends React.Component {
componentDidMount() {
// create map
this.map = L.map("map", {
center: [40.7610, -111.8829],
zoom: 13,
layers: [
L.tileLayer("http://{s}.tile.osm.org/{z}/{x}/{y}.png", {
attribution:
OpenStreetMap contributors'
})
]
});

}

render() {
return

;
}
}

export default Map;

Also, don't forget to set up style prop with the height and width of your Map component:

Full working example:

import { Map, TileLayer } from 'react-leaflet'
import 'leaflet/dist/leaflet.css'

const MyMap = () => (
  <Map
    style={{ width: '100%', height: '600px' }}
  >
    <TileLayer
      attribution='&amp;copy <a href=&quot;http://osm.org/copyright&quot;>OpenStreetMap</a> contributors'
      url='https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png'
    />
  </Map>
)

Yes! This is key, adding a style element for width and height directly on the component. The docs say to add this to the containing div which did not fix the problem (same problem as other posters in this thread). I did mine inline (same as @semanser above) and it worked great.

So:

  1. Make sure you import the style sheet: import "leaflet/dist/leaflet.css";
  2. Add width and height on at least the component. I did

Hope that helps!

-BWD.

To those having trouble getting the styles to display correctly, try adding:
<link href='https://cdnjs.cloudflare.com/ajax/libs/leaflet/1.0.3/leaflet.css' rel='stylesheet'>
to your index.html file.

If you're using webpack and have a file-loader present, you can instead load the css into a component with import 'leaflet/dist/leaflet.css'

This still doesn't solve my problem with the tiles. Still getting a messed up container with a messed up map even setting the style AND importing the style from leaflet.

I'm using webpack 4.30.0 and leaflet 1.4.0

@brunoclima In what way is the map messed up? Is it not displaying at all, or tiles are misaligned, or something else?

If you want to avoid inline style you can made it like this:

import React from "react";
import styled from "@emotion/styled";

import { Map, TileLayer } from "react-leaflet";

const MapContainer = styled(Map)`
  width: 100%;
  height: 400px;
`;

export default function ContactMap() {


  return (
    <MapContainer center={[49.50509, 8.49734]} zoom={13}>
      <TileLayer url="http://{s}.tile.osm.org/{z}/{x}/{y}.png" />
    </MapContainer>
  );
}

THANK YOU @semanser
SAVED MY DAY

    <Map
          style={{ width: '100%', height: '600px' }}
        >

Except that Map is not exported from react-leaflet.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

acpower7 picture acpower7  路  4Comments

lambdakris picture lambdakris  路  3Comments

Shadowman4205 picture Shadowman4205  路  4Comments

aemdeei picture aemdeei  路  3Comments

farahabdi picture farahabdi  路  3Comments