react-leaflet icon indicating copy to clipboard operation
react-leaflet copied to clipboard

React leaflet does not render properly, not functioning

Open inovramadani opened this issue 2 years ago • 19 comments

Bug report in v4

Expected behavior

Should function as leaflet JS.

Actual behavior

Broken maps, not functioning at all

Steps to reproduce

Go quickly to the sandbox and you will know quickly: https://codesandbox.io/s/react-leaflet-bug-sw6i8o?file=/src/App.js

image

inovramadani avatar Dec 12 '22 08:12 inovramadani

exactly same here

blitz2200 avatar Dec 12 '22 09:12 blitz2200

It's ok if I use:

[email protected]
[email protected]

inovramadani avatar Dec 12 '22 09:12 inovramadani

import "leaflet/dist/leaflet.css";

macrouch avatar Dec 16 '22 16:12 macrouch

it should be closed now.

almahdi404 avatar Dec 27 '22 18:12 almahdi404

Solved this issue by installing leaflet and import "leaflet/dist/leaflet.css";

Style files should be kept with react-leaflet package or mentioned in docs.

Arif-un avatar Dec 30 '22 14:12 Arif-un

I already tried every methods mention above but the map is still broken

HanlinTheDEVELOPER avatar Jan 12 '23 06:01 HanlinTheDEVELOPER

with [email protected] [email protected] I get [email protected] is required but both require react 17 and I'm on react 18. with react-leaflet 4.2.0 and leaftlet 1.9.3 without the CSS import I get fragmented maps not alligned. With the CSS import I get nothing at all

jcable avatar Jan 13 '23 16:01 jcable

I've got it working. The problem is that the mandatory height attribute has to be applied to the div that's rendered by the react MapContainer JSX element. It's not obvious and it would be really helpful if height were an explicit prop.

I've made it work by adding an id property to the MapContainer and giving that an explicit height style outside the react framework.

jcable avatar Jan 22 '23 10:01 jcable

So, if you are having the problems described above, you need to:

  1. Add: import 'leaflet/dist/leaflet.css'; to your react component
  2. Add style={{ height: 536 }}. Noticed, that height: 'style={{ height: '536px' }}' doesn't work

artem-ara avatar Feb 08 '23 13:02 artem-ara

So, if you are having the problems described above, you need to:

  1. Add: import 'leaflet/dist/leaflet.css'; to your react component
  2. Add style={{ height: 536 }}. Noticed, that height: 'style={{ height: '536px' }}' doesn't work

Confirming that this workaround also works with the latest React and Leaflet versions. Thanks!

fabian-0520 avatar Feb 09 '23 11:02 fabian-0520

I tried these. But i am gettings a blank

image


 <div  className='border-2'
        style={{
            maxWidth: '50vw',
            height: 536
        }}>
              <MapContainer center={position} zoom={13} scrollWheelZoom={false}>
    <TileLayer
      attribution=''
      url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
    />
    <Marker position={position}>
      <Popup>
        A pretty CSS3 popup. <br /> Easily customizable.
      </Popup>
    </Marker>
  </MapContainer>
        </div>

fathah avatar Jun 11 '23 15:06 fathah

Import the css file and add this additionally

    .leaflet-container {
        height: 300px;
    }

Gyvastis avatar Jun 30 '23 09:06 Gyvastis

Seems to me this can be closed as adding height to .leaflet-container and importing leaflet/dist/leaflet.css solves the issue: https://codesandbox.io/s/react-leaflet-bug-forked-c7zf7s

barbalex avatar Jul 16 '23 11:07 barbalex

try this

import { MapContainer, TileLayer, Marker, Popup, useMap } from "react-leaflet";
import "leaflet/dist/leaflet.css";

const ComponentResize = () => {
	const map = useMap();

	setTimeout(() => {
		map.invalidateSize();
	}, 0);

	return null;
};


const Map = () => {
	const position = [36.0339, 1.6596];

	return (
		<MapContainer
			style={{
				height: "100%",
				width: "100%",
			}}
			center={position}
			attributionControl={true}
			zoom={8}
			minZoom={3}
			scrollWheelZoom={true}
		>
			<ComponentResize />
			<TileLayer
				// className={'ion-hide'}
				attribution='&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
				url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
			/>
			<Marker position={position}>
				<Popup>
					A pretty CSS3 popup. <br /> Easily customizable.
				</Popup>
			</Marker>
		</MapContainer>
	);
};

export default Map;

issam-seghir avatar Nov 29 '23 20:11 issam-seghir

So, if you are having the problems described above, you need to:

1. Add: `import 'leaflet/dist/leaflet.css';` to your react component

2. Add `style={{ height: 536 }}`.  Noticed, that height: 'style={{ height: '536**px**' }}' **doesn't work**

This worked for me, the tile was broken but adding the fixed height fixed the problem. The height is up to you and doesn't have to be a fixed 536

oldtune avatar Dec 26 '23 00:12 oldtune

I just made the height of the mapcontainer 100%, any height given will display the map, and don't forget to use to CSS provided by leaflet mentioned above

      className="h-[100%]"
      center={[51.505, -0.09]}
      zoom={13}
      scrollWheelZoom={true}
    >
      <TileLayer
        attribution='&copy; <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
        url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
      />
      <Marker position={[51.505, -0.09]}>
        <Popup>
          A pretty CSS3 popup. <br /> Easily customizable.
        </Popup>
      </Marker>
    </MapContainer>```

abid365 avatar Mar 06 '24 18:03 abid365

Fully working example for nextjs.

'use client';
import { LatLngExpression } from 'leaflet';
import { MapContainer, Marker, Popup, TileLayer } from 'react-leaflet';
import 'leaflet/dist/leaflet.css';

type MapProps = {
  position: LatLngExpression;
};

export default function Map({ position }: MapProps) {
  return (
    // Height or width must be set for the map to display
    <div className="w-full">
      <MapContainer center={position} zoom={13} scrollWheelZoom={false} className="h-full">
        <TileLayer
          attribution='&copy; <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
          url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
        />
        <Marker position={position}>
          <Popup>
            A pretty CSS3 popup. <br /> Easily customizable.
          </Popup>
        </Marker>
      </MapContainer>
    </div>
  );
}

To avoid the error of window missing use dynamic import.

const Map = dynamic(() => import('@/components/map'), {
  ssr: false,
  loading: () => <p>Loading...</p>,
});

ImLunaHey avatar Mar 24 '24 04:03 ImLunaHey

I dont know why but it is very annoying problem. i have the same issue and i ensure you that there is nothing left that i can try to solve this problem. what i discovered and end up with is that it does work only if you set up with create-react-app, it doesnt when you set up with vite. so i dont know how to solve this for vite, maybe in the vite.config.js file.

import { MapContainer, Marker, Popup, TileLayer } from 'react-leaflet';
import 'leaflet/dist/leaflet.css';

export default function ContactLocation() {

  return (
    <section>
      <div className="container max-w-full">
        <MapContainer
          center={[-3.745, -38.523]}
          zoom={13}
          scrollWheelZoom={false}
          className="h-full"
        >
          <TileLayer
            attribution='&copy; <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
            url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
          />
          <Marker position={[-3.745, -38.523]}>
            <Popup>
              A pretty CSS3 popup. <br /> Easily customizable.
            </Popup>
          </Marker>
        </MapContainer>
      </div>
    </section>
  );
}

looks like this

ZeynalliZeynal avatar May 09 '24 20:05 ZeynalliZeynal

I dont know why but it is very annoying problem. i have the same issue and i ensure you that there is nothing left that i can try to solve this problem.

import { MapContainer, Marker, Popup, TileLayer } from 'react-leaflet';
import 'leaflet/dist/leaflet.css';

export default function ContactLocation() {

  return (
    <section>
      <div className="container max-w-full">
        <MapContainer
          center={[-3.745, -38.523]}
          zoom={13}
          scrollWheelZoom={false}
          className="h-full"
        >
          <TileLayer
            attribution='&copy; <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
            url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
          />
          <Marker position={[-3.745, -38.523]}>
            <Popup>
              A pretty CSS3 popup. <br /> Easily customizable.
            </Popup>
          </Marker>
        </MapContainer>
      </div>
    </section>
  );
}

looks like this

when i dont import css it looks like this image

ZeynalliZeynal avatar May 09 '24 20:05 ZeynalliZeynal