react-leaflet-enhanced-marker icon indicating copy to clipboard operation
react-leaflet-enhanced-marker copied to clipboard

Icons drift when zooming

Open Alby407 opened this issue 3 years ago • 6 comments

If I zoom out of the map, the icons (in my case Material UI Icons as React Components) drift from their intended position.

Alby407 avatar Feb 23 '22 23:02 Alby407

I am having the same problem. Were you able to fix the issue by playing with the icon anchoring or did you find another solution?

leoc avatar Mar 17 '22 15:03 leoc

Looking into it. Is it possible to send a video or gif that shows the bug?

On Thu, Mar 17, 2022 at 8:46 AM Arthur Leonard Andersen < @.***> wrote:

I am having the same problem. Were you able to fix the issue by playing with the icon anchoring or did you find another solution?

— Reply to this email directly, view it on GitHub https://github.com/ishaan6395/react-leaflet-enhanced-marker/issues/32#issuecomment-1070998521, or unsubscribe https://github.com/notifications/unsubscribe-auth/AHLS7XYIMTMM6KK2CL5HHSTVANHVXANCNFSM5PFYV23Q . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

You are receiving this because you are subscribed to this thread.Message ID: @.*** com>

ishaan6395 avatar Mar 17 '22 15:03 ishaan6395

Peek 2022-03-17 16-59

Thank you for the swift reply!

The marker should stay in the map center.

Maybe you could give a hint about how I can pass marker anchors via the react components. :-)

leoc avatar Mar 17 '22 16:03 leoc

Maybe it's this line: https://github.com/ishaan6395/react-leaflet-enhanced-marker/blob/57236aab2fe296dbe2ed9b2a0009c4c6778c34f1/src/components/Marker/Utilities/IconUtils.js#L6

The width of the leaflet div seems to be fixed. Also the divIcon should get some popupAnchor and iconAnchor properties or do you set them elsewhere?

Thanks a lot for your help :+1:

leoc avatar Mar 17 '22 16:03 leoc

I fixed the problem by replacing the enhanced marker with this component:

Marker.jsx:

import { Marker } from "react-leaflet";
import L from "leaflet";
import React from "react";
import ReactDOMServer from "react-dom/server";

export default ({
  iconSize,
  iconAnchor,
  popupAnchor,
  icon,
  children,
  ...props
}) => {
  return (
    <Marker
      icon={L.divIcon({
        className: "custom icon",
        iconSize,
        iconAnchor,
        popupAnchor,
        html: ReactDOMServer.renderToString(icon),
      })}
      {...props}
    >
      {children}
    </Marker>
  );
};

Using:

<MapContainer>
  <Marker
    position={position}
    iconSize={[45.25, 45.25]}
    iconAnchor={[16, 40]}
    popupAnchor={[0, -50]}
    icon={icon}
  >
    <Popup>{popup}</Popup>
  </Marker>
</MapContainer>

Now cool would be to fetch the size of the icon component automatically and picking anchors dynamically :-)

leoc avatar Mar 17 '22 20:03 leoc