Reputation: 8054
The react-leaflet
map does not get rendered properly.
The problem occurs when using the map with standard react
components.
My site also uses react-bootstrap
. As I have read this may cause some potential problems to how react-leaflet
gets rendered.
import React from 'react';
import ReactDOM from 'react-dom';
import { Map, Marker, Popup, TileLayer } from 'react-leaflet';
const position = [37.335556, -122.009167];
class MapView extends React.Component {
render() {
return (
<div
style={{
height:"100%"
}}>
<Map center={position} zoom={13}>
<TileLayer
url='http://{s}.tile.osm.org/{z}/{x}/{y}.png'
attribution='© <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>
</div>
);
}
}
module.exports = MapView;
Upvotes: 19
Views: 15863
Reputation: 31
You definitely not imported leaflet CSS file. Use this:
import 'leaflet/dist/leaflet.css'
Upvotes: 2
Reputation: 19
Maybe you can try this, it for React.js or Next.js, right now it's working on react.js 18.
First i recommend to uninstall any leaflet library then install this one
npm i leaflet leaflet-defaulticon-compatibility leaflet-geosearch
You can use this code:
import React, { useState, useEffect, useRef, useMemo, useCallback } from 'react'
const markerRef = useRef(null)
const eventHandlers = useMemo(
() => ({
dragend() {
const marker = markerRef.current
if (marker != null) {
// setPosition(marker.getLatLng())
console.log(marker.getLatLng())
}
},
}),
[],
)
useEffect(() => {
// you will get the current position of the marker here
console.log(markerRef.current)
}, [markerRef.current])
//////////////////////
/// in the html
<MapContainer center={[40.8054, -74.0241]} zoom={14} scrollWheelZoom={false} style={{ height: "400px", width: "100%" }}>
<TileLayer
attribution='© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png" />
<Marker
eventHandlers={eventHandlers}
position={[40.8054, -74.0241]}
draggable={true}
animate={true}
ref={markerRef}
>
<Popup>
Hey ! you found me
</Popup>
</Marker>
</MapContainer>
Copied from here
Upvotes: -1
Reputation: 862
first at all, install via console:
> npm install leaflet
> npm install react-leaflet
in the index.js import a css wich is living in node_modules
//src/index.js
import 'leaflet/dist/leaflet.css'
so, now just put a margin and height:
////src/App.js
return (
<MapContainer center={position} zoom={3} scrollWheelZoom={false}
style={{ height:"`enter code here`400px",backgroundColor:"red",marginTop:"80px", marginBottom:'90px'
}} >
</MapContainer>
)
or
return (
<>
<Map style={{ width: "100%", height: "100vh" }} center={center} zoom={13}>
</Map>
</> )
Upvotes: 28
Reputation: 131
Also add these Leaflet's CSS and JS files into your project
<link rel="stylesheet" href="https://unpkg.com/[email protected]/dist/leaflet.css" />
<script src="https://unpkg.com/[email protected]/dist/leaflet.js"></script>
Also add height to the map. it is mandatory.
Simple JsFiddle P.S. look into external resources.
Upvotes: 10
Reputation: 8054
The main problem was that the CSS was not being imported and height for the Map component was not set.
I then fixed a problem with the missing Marker image by using react-leaflet-marker-layer
import React from 'react';
import ReactDOM from 'react-dom';
import { Map, Marker, Popup, TileLayer } from 'react-leaflet';
import MarkerLayer from 'react-leaflet-marker-layer';
const position = { lng: -122.673447, lat: 45.522558 };
const markers = [
{
position: { lng: -122.67344700000, lat: 45.522558100000 },
text: 'Voodoo Doughnut',
},
{
position: { lng: -122.67814460000, lat: 45.5225512000000 },
text: 'Bailey\'s Taproom',
},
{
position: { lng: -122.67535700000002, lat: 45.5192743000000 },
text: 'Barista'
},
{
position: { lng: -122.65596570000001, lat: 45.5199148000001 },
text: 'Base Camp Brewing'
}
];
class ExampleMarkerComponent extends React.Component {
render() {
const style = {
border: 'solid 1px lightblue',
backgroundColor: '#333333',
borderRadius: '50%',
marginTop: '-5px',
marginLeft: '-5px',
width: '10px',
height: '10px'
};
return (
<div style={Object.assign({}, this.props.style, style)}></div>
);
}
}
class MapView extends React.Component {
render() {
return (
<div
style={{
height:"700px"
}}>
<Map center={position} zoom={13}
style={{
height:"700px"
}}>
<TileLayer
url='http://{s}.tile.osm.org/{z}/{x}/{y}.png'
attribution='© <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
/>
<MarkerLayer
markers={markers}
longitudeExtractor={m => m.position.lng}
latitudeExtractor={m => m.position.lat}
markerComponent={ExampleMarkerComponent} />
</Map>
</div>
);
}
}
module.exports = MapView;
Upvotes: 8