Giter Site home page Giter Site logo

Comments (8)

sethii avatar sethii commented on May 26, 2024 9

you're missing css import
import 'leaflet/dist/leaflet.css';

from react-leaflet.

comp615 avatar comp615 commented on May 26, 2024 2

FWIW, investigated this a bit. Basically it's never going to find the icon because you need to dig it out of the node_modules/dist. Did not look at source code, but for a workaround, you can reconstruct the default icon depending on your bundler / asset serving:

import { Icon } from "leaflet";

const defaultIcon = new Icon({
  iconUrl:
    "https://cdnjs.cloudflare.com/ajax/libs/leaflet/1.7.1/images/marker-icon.png",
  iconSize: [25, 41],
  iconAnchor: [12, 41],
  popupAnchor: [1, -34],
  shadowSize: [41, 41],
});

<Marker icon={defaultIcon} position={position}>

Note that iconUrl is just a Copilot generated random link. You could use your own here, or the ACTUAL file is located like so:

import defaultIconPng from "leaflet/dist/images/marker-icon.png";

But you'll need some way to actually serve that since I don't think leaflet likes the raw data. Hopefully that's a start towards a better example

from react-leaflet.

nakajimayoshi avatar nakajimayoshi commented on May 26, 2024 1

unfortunately importing leaflet/dist/leaflet.css causes the entire map to disappear for me. Not importing it leads to incomplete/scattered map tiles. Any fix for this? I'm using Vite/React+TS

Edit

Found a solution. Add this to your styles.css and make sure it's imported in the app root (in Vite's case main.tsx) in addition to the dist/leaflet.css import

#map {
  height: 100vh;
}

.leaflet-container {
  height: 600px;
  width: 100%;
}

.leaflet-div-icon {
  background: transparent;
  border: none;
}

from react-leaflet.

kthehatter avatar kthehatter commented on May 26, 2024

same issue with nextjs 13

from react-leaflet.

bhaidar avatar bhaidar commented on May 26, 2024

Thanks @sethii This works! I am also not seeing the marker icons. Any more files needed to be loaded? Thanks

from react-leaflet.

bhaidar avatar bhaidar commented on May 26, 2024

Hey @sethii
It seems StackBlitz and CodeSandbox use their own url for the market icon. I found a trick online that does the job:

Sample

However, when the map first loads, it loads part of it, I have then to resize the window to see a full map. Is there something I am missing here?

Thanks

from react-leaflet.

zacharytyhacz avatar zacharytyhacz commented on May 26, 2024

image

from react-leaflet.

zacharytyhacz avatar zacharytyhacz commented on May 26, 2024

Excellent work @comp615 , that works for me brother.

However, I did have to add in a TypeScript ignore because TypeScript was getting upset that the file path did not exist ā€“ but it does work and fix the issue.

// @ts-ignore because this image needs to be imported, but not found for some reason, but works.
import defaultIconPng from 'leaflet/dist/images/marker-icon.png'

const defaultIcon = new Icon({
  iconUrl: defaultIconPng,
  iconSize: [25, 41],
  iconAnchor: [12, 41],
  popupAnchor: [1, -34],
  shadowSize: [41, 41],
});

image

from react-leaflet.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    šŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. šŸ“ŠšŸ“ˆšŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ā¤ļø Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.