Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

react-leaflet map not correctly displayed

I'm trying to use react-leaflet to display a map. I use the code from this fiddle which is working, but on my computer I have this output

enter image description here

Here is my code :

DeviceMap.js

import React from 'react' import { Map, Marker, Popup, TileLayer } from 'react-leaflet';  export class DeviceMap 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} scrollWheelZoom={false}>         <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>     );   } }  export default DeviceMap 

DeviceTabs.js

export class DeviceTabs extends React.Component {   state = {     index: 0   };    handleTabChange = (index) => {     this.setState({ index })   };    render () {     return (       <Tabs index={this.state.index} onChange={this.handleTabChange}>         <Tab label='Values'>           <DeviceTable {...this.props} />         </Tab>         <Tab label='Map'>           <div className={style.leaflet}>             <DeviceMap />           </div>         </Tab>       </Tabs>     )   } } 

style.scss

.leaflet {   height: 300px;   width: 100%; } 

There is no error in the console, and I have no more idea where to search. Since the fiddle is working it is not a bug. Did I miss something ?

like image 820
ThomasThiebaud Avatar asked Nov 01 '16 17:11

ThomasThiebaud


People also ask

Is react leaflet good?

Leaflet and its React counterpart, React Leaflet, are a fantastic open source and free mapping alternative to Google Maps and MapBox, no API key required! It is an easy package to work with and one worth trying out.

How do you load a leaflet in CSS?

You can do it by selecting the leaflet-container class in CSS, which is by default included in the container div. Or, you can set an id or your own class for the <MapContainer /> component and reference it in the CSS. Here is an example using default class leaflet-container . Voila, you should then see the map.


1 Answers

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

like image 142
rambossa Avatar answered Oct 06 '22 10:10

rambossa