When creating a Marker onClick, the marker is offset and not directly where the mouse cursor is

Viewed 627

I'm trying to create a react-leaflet Map that onClick creates a Marker on the click's Lat and Lng. The problem is that the marker is created at an offset, towards the bottom right instead of on the mouse cursor.

import React, { useState, useContext } from "react";
import classes from "./WorldMap.module.css";
import { Map, TileLayer, Marker } from "react-leaflet";
import L from "leaflet";
import RoundInformationDisplay from "./RoundInfoDisplay/RoundInfoDisplay";
import ConfirmGuessButton from "./ConfirmGuessButton/ConfirmGuessButton";
import { AppContext } from "../../../../context/appContext";
interface Props {
  handleEndOfRound: () => void;
}
const WorldMap: React.FC<Props> = (props) => {
  const { currentRoundGuess, setCurrentRoundGuess } = useContext(AppContext);
  const icon = L.icon({ iconUrl: require("../../../../static/marker.svg") });

  const [selected, setSelected] = useState<boolean>(false);
  const handleMapClick = (e: any) => {
    setCurrentRoundGuess({ ...e.latlng });
    setSelected(true);
  };

  return (
    <div className={classes.WorldMapContainer}>
      <div className={classes.InfoDisplay}>
        <RoundInformationDisplay />
      </div>
      <Map
        center={[45.4, -75.7]}
        zoom={3}
        className={classes.Map}
        onClick={(e: any) => handleMapClick(e)}
      >
        {selected ? (
          <Marker
            position={[currentRoundGuess.lat, currentRoundGuess.lng]}
            icon={icon}
          />
        ) : (
          <p></p>
        )}
        <TileLayer url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png" />
      </Map>
      <div className={classes.ConfirmButton}>
        <ConfirmGuessButton
          selected={selected}
          handleEndOfRound={props.handleEndOfRound}
        />
      </div>
    </div>
  );
};
export default WorldMap;

A video showcasing the issue: https://streamable.com/g5uusl

What could be causing this problem? Thank you in advance.

1 Answers

As was mentioned in comments icon.iconanchor property allows to align the icon position relative to marker's location, in case of react-leaflet library, icon.iconanchor property could be specified as demonstrated below:

function MapExample(props) {
  const [selectedPos, setSelectedPos] = useState(props.center);
  

  function getMarkerIcon() {
    const icon = new L.Icon({
      iconSize: [32, 37], // size of the icon
      iconAnchor: [16, 37], 
      iconUrl: "/bigcity_green.png",
    });
    return icon;
  }

  function handleMapClick(e) {
    setSelectedPos(e.latlng);
  }

  return (
    <Map center={props.center} zoom={props.zoom} onclick={handleMapClick}>
      <TileLayer
        url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
        attribution='&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
      />
      {selectedPos && <Marker position={selectedPos} icon={getMarkerIcon()} />}
    </Map>
  );
}
Related