I'm trying to use a custom icon
with markers
with react-leaflet
Following the indications found here: https://leafletjs.com/examples/custom-icons/ and here: https://roblahoda.com/blog/leaflet-react-custom-icons/
I added these lines of code:
import Leaflet from 'leaflet'
import "leaflet/dist/leaflet.css"
import React from 'react'
import {
TileLayer,
LayersControl,
LayerGroup,
useMap,
useMapEvents,
GeoJSON,
Marker,
Popup,
} from 'react-leaflet'
export const newicon = new Leaflet.Icon({
iconUrl: ("../../..assets/marker.svg"),
iconAnchor: [5, 55],
popupAnchor: [10, -44],
iconSize: [25, 55],
})
And for the Marker
I specified the newicon
:
<Marker position={getMarkerPosition(state_name)} icon={newicon}>
(base) raphy@pc:~/Raphy-Template/src/assets$ ls -lah
total 56K
-rw-rw-r-- 1 raphy raphy 5.0K Jan 31 11:40 marker.png
But the new icon is not properly rendered:
Update 1)
Using require
:
export const newicon = new Leaflet.Icon({
iconUrl: require("../../../assets/marker.svg"),
iconAnchor: [5, 55],
popupAnchor: [10, -44],
iconSize: [25, 55],
})
I get this error:
Module not found: Error: Can't resolve '../../../assets/marker.svg'
With import
instead if require
I do not get any error, but still the icon doesn't show up:
export const newicon = new Leaflet.Icon({
// @ts-ignore
iconUrl: import("../../../assets/svg/push_pin_black_36dp.svg"),
iconAnchor: [5, 55],
popupAnchor: [10, -44],
iconSize: [25, 55],
})
(base) raphy@pc:~/Raphy-Template/dist/assets/svg$ ls -lah
-rw-rw-r-- 1 raphy raphy 390 Jan 31 19:05
push_pin_black_36dp.svg
(base) raphy@pc:~/Raphy-Template/dist/assets/svg$
Other info:
I've put the code inside this CodeSandbox
repo: https://codesandbox.io/s/damp-tdd-i917u?file=/src/App.js
"react": "^17.0.2"
"react-leaflet": "^3.2.5"
node: v16.13.0
O.S.: Ubuntu 20.04 Desktop
How to make the custom icon
appear?