Search code examples
reactjsnext.jsreact-leaflet

React Leaflet marker icon isn't showing my next.js app


I am using react leaflet my next.js app.React Leaflet marker is broken see image. I am using custom market but not work. I 've put together a very simple React / Leaflet demo but the marker icon is not showing at all

import { useState, useEffect, useRef } from "react";
import "leaflet/dist/leaflet.css";
import {
  MapContainer,
  TileLayer,
  Marker,
  Popup,
  useMap,
  useMapEvents,
} from "react-leaflet";
import { useSelector } from "react-redux";
import { useCallback } from "react";
import { useMemo } from "react";


function LocationMarker({ lat, lng }) {
  const [position, setPosition] = useState(null);
  const [bbox, setBbox] = useState([]);
  const map = useMap();

  useEffect(() => {
    map.flyTo([lat, lng], map.getZoom());
  }, [lat]);

  return position === null ? null : (
    <Marker position={position}>
      <Popup>You are here</Popup>
    </Marker>
  );
}

export default function EditAddedMap({ lat, lng, setLat, setLng }) {
  const { resturant } = useSelector((state) => state);

  return (
    <MapContainer center={[lat, lng]} zoom={13} scrollWheelZoom={false}>
      <TileLayer
        attribution='&copy; <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
        url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
      />
      <LocationMarker lat={lat} lng={lng} />
      <DraggableMarker lat={lat} lng={lng} setLng={setLng} setLat={setLat} />
    </MapContainer>
  );
}

enter image description here


Solution

  • Try to install leaflet-defaulticon-compatibility found here https://www.npmjs.com/package/leaflet-defaulticon-compatibility

    And import it accordingly:

    import "leaflet-defaulticon-compatibility";
    import "leaflet-defaulticon-compatibility/dist/leaflet-defaulticon-compatibility.css";