1

I have been trying to create a map section in my website for at least an hour now, but the react-leaflet or leaflet just won't work at all. I do every step that the doc says, I have watched a lot of videos, checked the other questions on the website and tried them, and I'm pretty sure that I do everything correct but it is the same for me. My computer has no problem, all necessary libs are installed in case you may ask (I'm using vite).

i was expecting this result:

i was expecting this result

Please, don't hesitate to tell me what may cause this irritating problem if you know.

this is the component.

import { MapContainer, Marker, Popup, TileLayer } from 'react-leaflet';
import 'leaflet/dist/leaflet.css';
const position = [40, 0];
export default function ContactLocation() {
  return (
    <section>
      <div className="container max-w-full h-[600px]">
        <MapContainer
          center={position}
          zoom={15}
          scrollWheelZoom={true}
          className="h-[80vh]"
        >
          <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>
    </section>
  );
}

this is the result:

this is the result

the problem is with the vite. i have wrote the same code using create-react app, it worked. so the main problem right now is what to do for vite and i have no idea. this is the vite.config.js file

import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'
import * as path from 'path'

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [react()],
  resolve: {
    alias: {
      '~': path.resolve(__dirname, 'src'),
    },
  },
})

7
  • In your browser's network tab, you should see the list of tiles requested from OpenstreetMap, if you click on one, what do you see (request url, params, etc.) ? Commented May 9 at 14:11
  • is it possible that vite may cause this problem? because i've tried every single way but still the same. i have tried the previous versions of leaflet but no luck. Commented May 9 at 14:31
  • 1
    I don't know, maybe try comparing your code with some of the examples here : codesandbox.io/examples/package/react-leaflet Commented May 9 at 14:32
  • Sir, i have also checked them, and i just discovered source of the issue. the problem is probably due to vite as i thought. So, I created a new project using create-react-app and wrote the same code in there, it worked. now i have no idea what to do about vite configuration, but im guessing you may know something to solve 🙏🏼 Commented May 9 at 15:17
  • I don't know, do you have errors in your browser's console ? Commented May 9 at 15:25

0

Browse other questions tagged or ask your own question.